I am trying to build an R package with Rcpp
code which uses an external library. I had previously asked SO about how to use an external C library in a package here. The problem I have facing is as soon as I include the following line of code
y = N_VNew_Serial(3);
I am getting the 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 get an error with the line
N_Vector y = NULL;
so, I think the connection to the library is working fine. I have also confirmed that the function declaration for N_VNewSerial()
is in the nvector/nvector_serial.h
In case you need to look at entire package code, it is available here
The code for the particular Rcpp
file is 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 am not sure why the code is reporting undefined reference
to one function but not to another in the same header file though, and any help in understanding the solving this error would be highly appreciated.
Thanks!
SN248
That is a link error and not a compile error. The compilation succeeded letting you get to the link step. 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
is very clear: R cannot build a shared library because you did not link against the object code (from Sundials, I presume) which provides it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With