Adding an external library in R package using Rcpp

情到浓时终转凉″ 提交于 2019-11-29 15:25:44

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/37753149/adding-an-external-library-in-r-package-using-rcpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!