Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling C++ in Rcpp with external C library

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

like image 849
Satya Avatar asked Jun 14 '16 18:06

Satya


1 Answers

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.

like image 119
Dirk Eddelbuettel Avatar answered Oct 16 '22 11:10

Dirk Eddelbuettel