Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a R package which use Rcpp with external c++ libraries?

Tags:

rcpp

Such as boost, where can I specify the following:

1.External c++ header file include path 
2.External c++ source file 
3.External c++ link library file path
like image 385
Xiaobo Gu Avatar asked Sep 02 '13 10:09

Xiaobo Gu


People also ask

What does the RCPP package do?

The 'Rcpp' package provides R functions as well as C++ classes which offer a seamless integration of R and C++. Many R data types and objects can be mapped back and forth to C++ equivalents which facilitates both writing of new code as well as easier integration of third-party libraries.

Can you use C++ in R?

We can use the function sourceCpp to read a function written in C++ into R interactively. The function takes care of the compilation using R CMD SHLIB and automatically generates an R wrapper for the underlying function.

Are R packages compiled?

Packages are collections of R functions, data, and compiled code in a well-defined format, created to add specific functionality. There are 10,000+ user contributed packages and growing.


2 Answers

It all goes into src/Makevars as explained in

  • the fine manual Writing R Extensions that came with R

  • either the Writing a package using Rcpp vignette or my book both of which I told you about in ...

  • ... my replies to your post on rcpp-devel

like image 107
Dirk Eddelbuettel Avatar answered Sep 22 '22 02:09

Dirk Eddelbuettel


Dirk's paper "Thirteen Simple Steps for Creating An R Package with an External C++ Library" gives an example src/Makevars:

CXX_STD = CXX11
PKG_CFLAGS = -I. -DGMP -DSKIP_MAIN
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -lgmpxx -lgmp

As you can see, additional libraries are specified in PKG_LIBS in this file. The src/Makevars approach assumes that you incorporate C++ code into your project using a standard package layout, as produced by Rcpp.package.skeleton(), with NAMESPACE and DESCRIPTION and so on.

According to Dirk's comments above, there is currently no way to specify an external library when C++ code is incorporated using the sourceCpp function, because that function provides an interface which is supposed to be multi-platform.

like image 38
Metamorphic Avatar answered Sep 23 '22 02:09

Metamorphic