Search code examples
rcpparmadillor-packagercpp11

Building R Package that uses RcppArmadillo, RcppEigen and depends on Cpp11 Plugins


I followed all the procedures explained so far about this matter either in this website or published notes by Dirk, Hadley or others. However, I still have problems in building my package due to the issue regarding cpp11 plugin.

I used RcppArmadillo.package.skeleton() function. I put my cpp file in the src directory. The NAMESPACE file looks as it should which contains importFrom(Rcpp, sourceCpp) line. I also edited DESCRIPTION file and in the LinkingTo section, I added RcppEigen and other packages I use. I finally ran the compileAttributes(verbose=TRUE) function in R and everything looked OK. Therefore, I think I have done everything as I should. I have to also mention that when I compile my code in R using sourceCpp(), it works perfect and is compiled with no errors! To illustrate better what my dependencies are, I put the first block of my code here:

    #include <RcppArmadillo.h>
    #include <RcppNumerical.h>
    #include <RcppArmadilloExtensions/sample.h>
    #include <Eigen/LU> 
    #include <algorithm>

    // [[Rcpp::depends(RcppArmadillo)]]
    // [[Rcpp::depends(RcppEigen)]]
    // [[Rcpp::depends(RcppNumerical)]]
    // [[Rcpp::plugins(cpp11)]]

The problem is when I build my package and I get errors and warnings for the lines I have auto type which relates to cpp11 plugin.

After searching similar posts on this website, I concluded that I have to force my R compiler to use c++11 and there fore I edited my Makvars file located at ~/.R/Makevars and since I use MAC I added this line: CXX=clang++ -std=c++11 to that file. However, when I do that those 3 errors go away but 50 new errors are generated as all of the Armadillo variable types, such as mat, uvec, etc are not recognized any more. So I don't know how to fix this.

I think basically putting // [[Rcpp::plugins(cpp11)]] should take care of it as the new version of Rcpp supports this plug in and probably that's why when I run sourceCpp in R I get no errors and everything looks fine. But I don't know what happens when building my package. My Rcpp version is 0.12.8 . Thank you in advance for any sorts of help.


Solution

  • Plugins for both dependencies (ie other headers) and compiler options are for use by sourceCpp().

    Packages do this with LinkingTo: and, for the C++11 directive, either src/Makevars or SystemRequirements. See Writing R Extensions which documents this.