6

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

Satya
  • 1,708
  • 1
  • 15
  • 39

1 Answers1

10

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
coatless
  • 20,011
  • 13
  • 69
  • 84
  • Thank you @Coatless and @nrussell. I added a `Makevars` and `Makevars.win` file in the `src` folder. I tried with both `#include ` and `#include "../inst/include/sundials/cvode/cvode.h" `(I changed the folder name to `sundials`). I am still getting the same error - `sundials_test.cpp:5:103: fatal error: ..inst/include/sundials/cvode/cvode.h: No such file or directory` – Satya Jun 10 '16 at 21:22
  • Thank you @Coatless. Your suggestion worked on my OS X machine, I haven't tried it on Windows yet. I have posted code at - https://github.com/sn248/Rcppsbmod/tree/master/src. The `/src/sundials_test.cpp` is not complete yet. I am getting a `C++ requires a type specifier for all declarations` error on `line 30` of the code (which is unrelated to this issue). The original complete code from the library that works on my OS X is at the gist - https://gist.github.com/sn248/b2e7726f609625df01f463715d5aa414. Hope you can help me in figuring out this error. Thanks. – Satya Jun 11 '16 at 12:20
  • Thank you very much @Coatless for the explanation and fixing the code. I will take a look at the pull request and carry on with trying to compile and run the example for the library. – Satya Jun 11 '16 at 19:36
  • Thank you @Coatless, with your help I was able to successfully `source` the `sundials_test.cpp` file. However, on running `load all` or `clean and rebuild` commands in `RStudio`, I am getting the error `ld: warning: directory not found for option '-L/usr/local/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2' ld: library not found for -lgfortran clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [Rcppsbmod.so] Error 1 ERROR: compilation failed for package ‘Rcppsbmod’`. Could you help me resolve this error? Thanks! – Satya Jun 13 '16 at 04:11
  • 1
    See my post here @SN248: http://thecoatlessprofessor.com/programming/r-compiler-tools-for-rcpp-on-os-x/ – coatless Jun 13 '16 at 04:12