Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Boost library in C++ with Rcpp

Tags:

c++

boost

rcpp

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.

I installed Boost in the directory /Users/giorgi/boost_1_53_0 therefore I set Sys.setenv("PKG_CXXFLAGS"="-I /Users/giorgi/boost_1_53_0") but I am not sure I am doing the right thing. Sorry but I am quite ignorant with this stuff!

like image 323
user2304364 Avatar asked Dec 05 '22 11:12

user2304364


1 Answers

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> 
like image 140
Dirk Eddelbuettel Avatar answered Dec 22 '22 06:12

Dirk Eddelbuettel