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

标签 c++ r rcpp r-package sundials

我正在尝试使用使用外部库的 Rcpp 代码构建 R 包。我之前曾问过关于如何在包 here 中使用外部 C 库的问题。我面临的问题是一旦包含以下代码行

y = N_VNew_Serial(3);

我遇到了错误

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

我没有收到这条线的错误

N_Vector y = NULL;

因此,我认为与图书馆的连接工作正常。我还确认了 N_VNewSerial() 的函数声明在 nvector/nvector_serial.h 中如果您需要查看整个包代码,可以使用 here

特定 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;


}

我不确定为什么代码在同一个头文件中报告一个函数的undefined reference 而不是另一个函数,如果能帮助理解解决这个错误,我们将不胜感激。

谢谢!

SN248

最佳答案

这是一个链接错误,而不是编译错误。编译成功让你进入链接步骤。但是

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

非常清楚:R 无法构建共享库,因为您没有链接提供它的目标代码(我想是来自日晷)。

关于c++ - 使用外部 C 库在 Rcpp 中编译 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37819967/

相关文章:

RcppRoll 或 CumSum 滞后于动态窗口

c++ - 类型和相关名称

r - strptime 不完全工作

Rcpp 警告 : "directory not found for option ' -L/usr/local/Cellar/gfortran/4. 8.2/gfortran'"

r - 阈值向量自回归模型的估计

r - dplyr 中单个数据的概率

c++ - Rcpp 和 move 语义

c++ - 系统::IO::目录::GetDirectories( "c: vs c:\\")

c++ - "."和 "->"运算符的混合链

c++ - 开始学习如何编写包含互斥量和信号量使用的极其简单的多线程 C++ 程序的最佳方法是什么?