Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

r - inline function code doesn't compile

I am trying to experiment with the inline library.

I tried the first example given at 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);
")

I get the following error:

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  

I tried evalCpp("2+2") from the Rcpp package and it gave me 4 as the answer. So, I guess there isn't anything wrong with the compiler per se. What could be the issue here?

I'm running Windows 7. My sessionInfo is as follows:

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

* EDIT *

Tried on another PC with Windows 7 where R is installed in a directory without any spaces and recently had R 3.1.0 installed. So it is definitely not due to the R path having spaces.

* EDIT *

I finally got this work! Needed to add the Rtools directories to PATH.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As hinted at in the question, you need to add two directories to the Windows PATH environment variable:

  1. The Rtools executable directory, probably in C:Rtoolsin.

  2. The gcc executable directory, probably in C:Rtoolsgcc-4.6.3in.

You'll likely need to reboot Windows in order for R to be able to see the new PATH.

You can add update the path environment variable for the current R session only, using something like:

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 = ";"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...