【问题标题】:inline function code doesn't compile内联函数代码无法编译
【发布时间】:2014-06-02 05:55:16
【问题描述】:

我正在尝试使用 inline 库。

我尝试了http://adv-r.had.co.nz/C-interface.html 给出的第一个示例

library(inline)

add <- cfunction(signature(a = "integer", b = "integer"), "
  SEXP result;

  PROTECT(result = allocVector(REALSXP, 1));
  REAL(result)[0] = asReal(a) + asReal(b);
  UNPROTECT(1);

  return(result);
")

我收到以下错误:

Warning message:
running command 'make -f "C:/PROGRA~1/R/R-30~1.3/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-30~1.3/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="file21e05c17451a.dll" WIN=64 TCLBIN=64 OBJECTS="file21e05c17451a.o"' had status 127 

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
  1: #include <R.h>
  2: #include <Rdefines.h>
  3: #include <R_ext/Error.h>
  4: 
  5: 
  6: extern "C" {
  7:   SEXP file21e05c17451a ( SEXP a, SEXP b );
  8: }
  9: 
 10: SEXP file21e05c17451a ( SEXP a, SEXP b ) {
 11: 
 12:   SEXP result;
 13: 
 14:   PROTECT(result = allocVector(REALSXP, 1));
 15:   REAL(result)[0] = asReal(a) + asReal(b);
 16:   UNPROTECT(1);
 17:                  
 18:   return(result);
 19: 
 20:   warning("your C program does not return anything!");
 21:   return R_NilValue;
 22: }
Error in compileCode(f, code, language, verbose) : 
  Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/PROGRA~1/R/R-30~1.3/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-30~1.3/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="file21e05c17451a.dll" WIN=64 TCLBIN=64 OBJECTS="file21e05c17451a.o"' had status 127 
In addition: Warning message:
running command 'C:/PROGRA~1/R/R-30~1.3/bin/x64/R CMD SHLIB file21e05c17451a.cpp 2> file21e05c17451a.cpp.err.txt' had status 1  

我从Rcpp 包中尝试了evalCpp("2+2"),它给了我4 作为答案。所以,我想编译器本身没有任何问题。这可能是什么问题?

我运行的是 Windows 7。我的sessionInfo 如下:

R version 3.0.3 (2014-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] compiler  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] inline_0.3.13           stringr_0.6.2           rhdf5_2.6.0            
[4] RcppRoll_0.1.0          RcppArmadillo_0.4.200.0 Rcpp_0.11.1            
[7] data.table_1.9.2       

loaded via a namespace (and not attached):
[1] plyr_1.8.1     reshape2_1.2.2 tools_3.0.3    zlibbioc_1.8.0

* 编辑 *

在另一台装有 Windows 7 的 PC 上尝试过,其中 R 安装在没有任何空格的目录中,并且最近安装了 R 3.1.0。所以这绝对不是因为R 路径有空格。

* 编辑 *

我终于得到了这份工作!需要将Rtools 目录添加到PATH

【问题讨论】:

  • 我在我的 Windows 7 机器上遇到了完全相同的错误,但在一台正在运行的 debian 上却没有。

标签: r windows-7


【解决方案1】:

正如问题中所暗示的,您需要将两个目录添加到Windows PATH environment variable

  1. Rtools 可执行目录,可能在C:\Rtools\bin

  2. gcc 可执行目录,大概在C:\Rtools\gcc-4.6.3\bin

您可能需要重新启动 Windows 才能使 R 能够看到新的 PATH

您可以仅为当前 R 会话添加更新路径环境变量,使用类似:

rtools <- "C:\\Rtools\\bin"
gcc <- "C:\\Rtools\\gcc-4.6.3\\bin"
path <- strsplit(Sys.getenv("PATH"), ";")[[1]]
new_path <- c(rtools, gcc, path)
new_path <- new_path[!duplicated(tolower(new_path))]
Sys.setenv(PATH = paste(new_path, collapse = ";"))

【讨论】:

  • 不需要重启Windows,重启R即可。
【解决方案2】:

我安装了 Rtools。之后它工作正常:https://cran.r-project.org/bin/windows/Rtools/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多