[Rcpp-devel] sourceCpp failing on windows whereas devtools::has_devel and devtools:::R works

romain at r-enthusiasts.com romain at r-enthusiasts.com
Thu Oct 17 09:04:20 CEST 2013


On a windows machine where Rtools is installed and works, i.e. I get:

> require(devtools)
> has_devel()
"C:/R/R-3.0.2/bin/i386/R" --vanilla CMD SHLIB foo.c

gcc -m32 -I"C:/R/R-30~1.2/include" -DNDEBUG    
-I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O3 -Wall  
-std=gnu99 -mtune=core2 -c foo.c -o foo.o
gcc -m32 -shared -s -static-libgcc -o foo.dll tmp.def foo.o 
-Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/R/R-30~1.2/bin/i386 -lR
[1] TRUE

sourceCpp does not manage to find the tools correctly:

> evalCpp( "1+1")

Error in sourceCpp(code = code, env = env, rebuild = rebuild, 
showOutput = showOutput,  :
   Error 1 occurred building shared library.

WARNING: The tools required to build C++ code for R were not found.

Please download and install the appropriate version of Rtools:


Using devtools:::R however, I manage to compile the file correctly:

> code <- '#include <Rcpp.h>
+ using namespace Rcpp;
+ // [[Rcpp::export]]
+ double foo(){
+        return 2;
+ }
+ '
> Sys.setenv( "PKG_CXXFLAGS" = Rcpp:::RcppCxxFlags() )
> Sys.setenv( "PKG_LIBS" = Rcpp:::RcppLdFlags() )
> writeLines( code, "test.cpp" )
> devtools:::R( "CMD SHLIB test.cpp", getwd() )
"C:/R/R-3.0.2/bin/i386/R" --vanilla CMD SHLIB test.cpp

g++ -m32 -I"C:/R/R-30~1.2/include" -DNDEBUG     
-IC:/R/R-3.0.2/library/Rcpp/include   -O2 -Wall  -mtune=core2 -c 
test.cpp -o test.o
g++ -m32 -shared -s -static-libgcc -o test.dll tmp.def test.o 
-Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/R/R-30~1.2/bin/i386 -lR

So perhaps we can borrow some wisdom out of devtools to make sourceCpp 
work. As one can expect, the code from sourceCpp that fails is these 

cmd <- paste(R.home(component = "bin"), .Platform$file.sep,
             "R ", "CMD SHLIB ", "-o ", shQuote(context$dynlibFilename),
             " ", ifelse(rebuild, "--preclean ", ""), 
             sep = "")


> version
platform       i386-w64-mingw32
arch           i386
os             mingw32
system         i386, mingw32
major          3
minor          0.2
year           2013
month          09
day            25
svn rev        63987
language       R
version.string R version 3.0.2 (2013-09-25)
nickname       Frisbee Sailing
> packageDescription("Rcpp")$Version
[1] "0.10.5"

More information about the Rcpp-devel mailing list