添加cpp11插件时出现错误消息"Undefined reference to boost (...)"

Error message "Undefined reference to boost (...)" comes when adding cpp11 plugin

在 R 中使用 Rcpp 编译我的 .cpp 文件时,出现此错误消息:

undefined reference to `boost::system::generic_category()'

但是当我删除 // [[Rcpp::plugins(cpp11)]] 行时,不再有任何错误。为什么?

这是我的最小可重现示例。

// include Rcpp, it takes care of most other headers you need
#include <Rcpp.h>
#include <boost/array.hpp>

// include Boost's odeint
#include <boost/numeric/odeint.hpp>
#include <boost/numeric/odeint/integrate/integrate_adaptive.hpp>
#include <boost/filesystem/fstream.hpp>
#include <functional>

// tell R you need Boost
// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(cpp11)]]

using namespace Rcpp;
using namespace std;
using namespace boost::numeric::odeint;

typedef boost::array< double ,130 > state_type;

// [[Rcpp::export]]
void my_fun22(Rcpp::NumericVector &x, const double t,const Rcpp::NumericVector theta){
  Function f("mod_cpp");
  x=f(_["t"]=t,_["x"]=x,_["p1"]=theta);
}

还有一个基本问题:Boost 系统(通常)需要链接,这与仅通过 BH 包指向 Boost headers 完全不同。非常标准的错误消息 undefined reference 来自链接器/定位符号失败的尝试。

我们在 Rcpp Gallery 上的一些帖子中讨论了链接到 Boost 库的使用,但缺点是没有可移植的方式 来提供链接跨 R 使用的操作系统提升库。