Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an external library in R package using Rcpp

Tags:

rcpp

r-package

I am trying to develop an R package which uses the Sundials C library for solving differential equations. In order to not have the user install the library, I am putting the source code of the library in my package.

I have put all the header files from the library in /inst/include/sundials-2.6.2 and the .c files in src/sundials-2.6.2 of my package folder.

From my reading of the SO posts on this topic, sourceCpp of code in multiple files (e.g., separate .h and .cpp files should work if they are structured to be a part of the package. I am trying to run a example code file from the Sundials package

My code (only the beginning part) looks something like

#include <Rcpp.h>

#include "../inst/include/sundials-2.6.2/cvode/cvode.h"             /* prototypes for CVODE fcts., consts. */
#include "../inst/include/sundials-2.6.2/nvector/nvector_serial.h"  /* serial N_Vector types, fcts., macros */
#include "../inst/include/sundials-2.6.2/cvode/cvode_dense.h"      /* prototype for CVDense */
#include "../inst/include/sundials-2.6.2/sundials/sundials_dense.h" /* definitions DlsMat DENSE_ELEM */
#include "../inst/include/sundials-2.6.2/sundials/sundials_types.h" /* definition of type realtype */

But, I am getting an error

fatal error: sundials/sundials_nvector.h: No such file or directory

I do example of something similar done in the following github repositories

Rcppsundials - https://github.com/AleMorales/RcppSundials.R/blob/master/src/cvode.cpp

which calls the header files using

#include <cvodes/cvodes.h>           // CVODES functions and constants
#include <nvector/nvector_serial.h>  // Serial N_Vector
#include <cvodes/cvodes_dense.h>     // CVDense

and has incorporated the header files under the /inst/include/ folder.

This is the first package I am trying to develop and I have not used C/C++ also extensively, so there could be something very silly in how I am trying to compile this program.

Just a side note - I was able to install and run an example on my OSX machine, but currently I am working from a Windows machine that does not have Sundials installed. It does have Rtools installed, so I can compile and run the example Rcpp programs.

Thank you SN

like image 835
Satya Avatar asked Jun 10 '16 16:06

Satya


1 Answers

External library linking should be done with the following setup:

R/
inst/
  |- include/
     |- sundials/ 
  |- header.h
src/
  |- sundials/
  |- Makevars
  |- Makevars.win
  |- action.cpp
man/
DESCRIPTION
NAMESPACE

Then add the following:

PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
PKG_CPPFLAGS =  -I../inst/include/ -I src/sundials

To both Makevars and Makevars.win

Here I've opted to remove the sundial version numbers from the folder names.

Edit

I've made the fixes necessary to compile the package:

https://github.com/sn248/Rcppsbmod/pull/1

Note:

The structure was:

inst/
  |- include/
     |- sundials/  
        |- arkode/
        .....
        |- nvector/  
        |- sundials/ 
  |- header.h

This would have forced the include statements to be:

#include <sundials/cvodes/cvodes.h>           // CVODES functions and constants
#include <sundials/nvector/nvector_serial.h>  // Serial N_Vector
#include <sundials/cvodes/cvodes_dense.h>     // CVDense

I changed it so that:

inst/
  |- include/
     |- arkode/
     .....
     |- nvector/  
     |- sundials/ 
  |- header.h

So, the statements will always be:

#include <cvodes/cvodes.h>           // CVODES functions and constants
#include <nvector/nvector_serial.h>  // Serial N_Vector
#include <cvodes/cvodes_dense.h>     // CVDense
like image 50
coatless Avatar answered Sep 24 '22 01:09

coatless