简体   繁体   中英

g++ errors when trying to compile c++11 with Rcpp

SYSTEM SPEC:

  • OS - Mac OS X 10.6.8 (Snow Leopard)
  • g++ - Macports gcc 4.8.1_2+universal
  • R - 2.15.3
  • Rcpp - 0.10.3

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?

Kevin Ushley is absolutely right - the easiest way to make sure that the right compiler is being used is through Makevars file. In my case, I added:

CXX = g++-4.8.1
PKG_CXXFLAGS = -std=c++11

This is what I have been missing - and it all worked afterwards. This works if you are compiling your package.

Quick ones:

  • you are behind on Rcpp which is at a released version 0.10.4

  • the version you are using (0.10.3) does have have a plugin for C++11

  • there is an entire article at the Rcpp Gallery which details this.

So allow me to quote from that C++11 piece on the Rcpp Gallery :

R> library(Rcpp)
R> sourceCpp("/tmp/cpp11.cpp")
R> useAuto()
[1] 42
R> 

where the code in /tmp/cpp11.cpp is as follows:

#include <Rcpp.h>

// Enable C++11 via this plugin (Rcpp 0.10.3 or later)
// [[Rcpp::plugins("cpp11")]]

// [[Rcpp::export]]
int useAuto() {
    auto val = 42;      // val will be of type int
    return val;
}

If that does not work for you, then your system is not set up right. In other words, this is not a question for the Rcpp tag -- but rather for 'how do I set up my path to invoke the version of g++ I think I should be invoking'.

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

#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

#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 wraps a C++ function FooBar defined and implemented in and respectively. 包含一个C ++函数 ,分别在定义和实现。

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM