Call “multi file C++ code with external libraries referenced” in R

后端 未结 2 519
失恋的感觉
失恋的感觉 2021-01-23 14:12

I have written a code in C++ which uses boost library and also uses multiple files. I am trying to execute the function in R.

Here is my C++ function prototype:

相关标签:
2条回答
  • 2021-01-23 14:50

    Since this a multi-file C++ project, you should package the code as an R package. It is actually quite easy to do using Rcpp, see for example this answer. The Rcpp-package vignette contains further information. In addition, an easy way to make use of boost is via the BH package. There are many example packages for using Rcpp + BH on CRAN which you can study, e.g. my own dqrng. Some more details can also be found in the R Packages book and of course in the official documentation Writing R Extensions.

    The crucial thing for using the BH package is LinkingTo: BH in the DESCRIPTION file. This ensures that -I<path_to_BH_package>/include is part of the compiler flags. In order to use header files that you place in inst/include, you should add PKG_CPPFLAGS = -I../inst/include to src/Makevars.

    0 讨论(0)
  • 2021-01-23 15:07

    Use Rcpp instead of .Call.

    Add this headers and comment to your .cpp function file:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    

    And now in R:

     install.packages("Rcpp")
     library(Rcpp)
     sourceCpp('path to your .cpp program')
    

    The function EulerInversions should become visible in the environment and you can call it now as any other R function

    0 讨论(0)
提交回复
热议问题