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

后端 未结 3 1827
生来不讨喜
生来不讨喜 2021-02-08 15:07

SYSTEM SPEC:

  • OS - Mac OS X 10.6.8 (Snow Leopard)
  • g++ - Macports gcc 4.8.1_2+universal
3条回答
  •  离开以前
    2021-02-08 15:29

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

提交回复
热议问题