Search code examples
c++rdependenciesrcpp

Understanding Rcpp plugins


I'm looking over the demo provided in 3.6 of the Rcpp-FAQ, and I'm trying to understanding how this plugin is being created. The standalone example provided is

gslrng <-
'int seed = Rcpp::as<int>(par) ;
gsl_rng_env_setup();
gsl_rng *r = gsl_rng_alloc (gsl_rng_default);
gsl_rng_set (r, (unsigned long) seed);
double v = gsl_rng_get (r);
gsl_rng_free(r);return Rcpp::wrap(v);'

plug <- Rcpp:::Rcpp.plugin.maker(
  include.before = "#include <gsl/gsl_rng.h>",
  libs = paste("-L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp",
               "-Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib",
               "-L/usr/lib -lgsl -lgslcblas -lm"))
registerPlugin("gslDemo", plug )
fun <- cxxfunction(signature(par="numeric"), gslrng, plugin="gslDemo")
fun(0)

Specifically, why is that call to paste() being comma-separated like that? Should all dependencies (header file directories, linker directories, and name of library files) be handled via plugins?


Solution

  • The paste() is a vanilla use of base R's paste() -- it creates a single string containing all the link instructions being passed to libs.

    For include.before we do not need that as only one header is passed.

    I would recommend you study the code along with its use as well as the actual plugin for RcppGSL which is defined here and see how they are used in packages such as e.g. RcppZiggurat. And by used I mean see what the instructions expand to when the client packages compiles and links.

    It looks crazy complicated but hey it has worked for about a decade already :)