SYSTEM SPEC:
I keep on receiving an error when I am trying to compile functions that use C++11 in R (through Rcpp) - for some reason, g++ does not recognise -std=c++11
option.
This example is taken from Rcpp help files (it does not contain anything specific to C++11, but can show what my problem is). If I try running:
require( Rcpp )
Sys.setenv( "PKG_CXXFLAGS"="-std=c++11" )
cppFunction(plugins=c("cpp11"), '
int useCpp11() {
int x = 10;
return x;
}')
I get:
cc1plus: error: unrecognized command line option "-std=c++11"
make: *** [file61239328ae6.o] Error 1
g++ -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 -DNDEBUG -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include" -std=c++11 -fPIC -g -O2 -c file61239328ae6.cpp -o file61239328ae6.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 1 occurred building shared library.
At the same time, I can compile this function directly from bash - if this code is in useCpp11.cpp
file, then this runs without any complaints:
g++ useCpp11.cpp -std=c++11
Certainly, I am doing something wrong, but I cannot work out what it is. gcc 4.8 is set as a default compiler in bash, Rcpp has been working without fault in the past. I suspect that I am not telling R which version of g++ to use - could that be the case?
To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.
To compile in C++11 mode, add the option –std=c++11 to the CC command line. The location on the command line is not important. The option causes the compiler to recognize language features new in C++11, and to use the C++11 version of the standard library (g++ runtime library that is supplied).
Status of Experimental C++11 Support in GCC 4.8GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extensions. As of GCC 4.8.
(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would.
If you don't want to fix up your path you can set the PKG_CXXFLAGS
environment variable just prior to compiling.
export PKG_CXXFLAGS='`Rscript -e "Rcpp:::CxxFlags()"` -std=c++11'
For example:
In R
#generate the bare Rcpp package as usual
library(Rcpp)
Rcpp.package.skeleton("Foo",
example_code=FALSE,
attributes=TRUE,
cpp_files=c("./Foo_R_wrapper.cpp", "./Foo.h", "/Foo.cpp")
)
and in Bash
#tell the compiler to use C++11
export PKG_CXXFLAGS='`Rscript -e "Rcpp:::CxxFlags()"` -std=c++11'
#compile as usual
cd ./Foo/;
R -e 'Rcpp::compileAttributes(".",verbose=TRUE)';
cd ..;
R CMD build Foo;
#ensure that there are no bugs
R CMD check Foo;
where Foo_R_wrapper.cpp wraps a C++ function FooBar defined and implemented in Foo.h and Foo.cpp respectively.
Foo_R_wrapper.cpp could contain the following:
#include <Rcpp.h>
#include "Foo.h"
// [[Rcpp::export]]
SEXP FooBar(SEXP baa)
{
Rcpp::NumericVector baa_vec(baa)
//Do something from Foo.h (to baa_vec?)
...
//etc
return baa_vec;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With