How to use Boost library in C++ with Rcpp

boost, c++, rcpp

Solution

I would try a few things:

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.

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

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()`.

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> 

Problem

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!

Original source