简体   繁体   English

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

[英]Compiling C++ in Rcpp with external C library

I am trying to build an R package with Rcpp code which uses an external library. 我正在尝试使用Rcpp代码构建一个R包, Rcpp代码使用外部库。 I had previously asked SO about how to use an external C library in a package here . 我之前曾问过如何在这里使用外部C库。 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 我还确认N_VNewSerial()的函数声明在nvector/nvector_serial.h 。如果你需要查看整个包代码,可以在这里找到

The code for the particular Rcpp file is paste below 特定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;


}

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. 我不知道为什么代码报告对一个函数的undefined reference但不报告在同一个头文件中的另一个函数,并且理解解决此错误的任何帮助都将受到高度赞赏。

Thanks! 谢谢!

SN248 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. 非常清楚:R无法构建共享库,因为您没有链接到提供它的目标代码(来自Sundials,我推测)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM