Compiling C ++ in Rcpp with an External C Library

I am trying to create an R package with Rcpp code that uses an external library. I previously asked about how to use the external C library in the package here . The problem I am facing is that I am including the following line of code

 y = N_VNew_Serial(3); 

I get an error

 sundials_test.o:sundials_test.cpp:(.text+0x2ba): undefined reference to `N_VNew_Serial' collect2: ld returned 1 exit status Error in Rcpp::sourceCpp("src/sundials_test.cpp") : Error occurred building shared library 

I do not receive error message with line

 N_Vector y = NULL; 

therefore, I think the connection to the library is working fine. I also confirmed that the function declaration for N_VNewSerial() is in nvector/nvector_serial.h . If you need to see all the package code, it is available here.

The code for a specific Rcpp file is in the paste below

 #include <Rcpp.h> // #include <iostream.h> #include <cvode/cvode.h> /* prototypes for CVODE fcts., consts. */ #include <nvector/nvector_serial.h> /* serial N_Vector types, fcts., macros */ #include <cvode/cvode_dense.h> /* prototype for CVDense */ #include <sundials/sundials_dense.h> /* definitions DlsMat DENSE_ELEM */ #include <sundials/sundials_types.h> /* definition of type realtype */ using namespace Rcpp; void InitialConditions (NumericVector x){ N_Vector y = NULL; // y = N_VNew_Serial(3); // // NV_Ith_S(y,0) = 1; // NV_Ith_S(y,1) = 2; // NV_Ith_S(y,2) = 3; } 

I'm not sure why the code provides an undefined reference one function, but not another in the same header file, although any help in understanding the solution to this error would be greatly appreciated.

Thanks!

SN248

+5
source share
1 answer

This is a link error, not a compilation error. After compilation, you managed to go to the link stage. But

 sundials_test.o:sundials_test.cpp:(.text+0x2ba): \ undefined reference to `N_VNew_Serial' collect2: ld returned 1 exit status Error in Rcpp::sourceCpp("src/sundials_test.cpp") : Error occurred building shared library 

very clear: R cannot create a shared library because you did not reference the object code (from the Sundials, I suppose) that provides it.

+6
source

All Articles