使用外部C库在Rcpp中编译C++

SN2*_*248 6 c++ r rcpp r-package sundials

我正在尝试Rcpp使用外部库构建一个包含代码的R包.我之前曾问过如何在这里使用外部C库.我遇到的问题是,只要包含以下代码行即可

y = N_VNew_Serial(3);
Run Code Online (Sandbox Code Playgroud)

我收到了错误

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
Run Code Online (Sandbox Code Playgroud)

我没有得到该行的错误

N_Vector y = NULL;
Run Code Online (Sandbox Code Playgroud)

所以,我认为与图书馆的连接工作正常.我还确认函数声明N_VNewSerial()是在nvector/nvector_serial.h你需要查看整个包代码的情况下,它可以在这里找到

特定Rcpp文件的代码粘贴如下

#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;


}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么代码报告undefined reference给一个函数而不报告给同一个头文件中的另一个函数,并且理解解决此错误的任何帮助都将受到高度赞赏.

谢谢!

SN248

Dir*_*tel 7

这是链接错误而不是编译错误.编译成功,让您进入链接步骤.但

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
Run Code Online (Sandbox Code Playgroud)

非常清楚:R无法构建共享库,因为您没有链接到提供它的目标代码(来自Sundials,我推测).

  • 在最后一个例子中,您使用了两个日(子)库:`-lsundials_cvode -lsundials_nvecserial`.你需要对你的R包做同样的事情,这是a)独立于你是否使用Rcpp和b)在写R扩展中清楚地记录. (2认同)