How to use Boost library in C++ with Rcpp

前端 未结 1 1732
滥情空心
滥情空心 2020-12-17 05:16

I am using Rcpp package on R 3.0.0. I am trying to run this example, but I cannot because I don\'t know how to use Boost.

相关标签:
1条回答
  • 2020-12-17 05:35

    I would try a few things:

    1. Write a three line standalone C++ program using Boost, and compile it. This is just to prove to yourself that you have the -I/some/dir flag right.

    2. Write a simple Rcpp function and use eg sourceCpp() to compile and load it.

    3. Create a file ~/.R/Makevars and set the -I flag from 1. here as either one one of CXXFLAGS or CFLAGS both of which will be used by R CMD ... and hence sourceCpp().

    4. If everything else fails, create a small package and add LinkingTo: BH as the CRAN package BH provides Boost headers you can use (once you install BH).

    Edit, about 1 1/2 years later

    You can also use a // [[Rcpp::depends(BH)]] as eg in this code

    #include <Rcpp.h>
    #include <boost/math/common_factor.hpp>  // included in BH  
    
    // [[Rcpp::depends(BH)]]    
    
    using namespace Rcpp;
    
    // [[Rcpp::export]]   
    int computeGCD(int a, int b) {
      return boost::math::gcd(a, b);
    }
    

    which builds and runs just fine as we updated both Rcpp and BH in the meantime:

    R> library(Rcpp)
    R> sourceCpp("/tmp/simpleBoost.cpp")
    R> computeGCD(6, 15)
    [1] 3
    R> 
    
    0 讨论(0)
提交回复
热议问题