尝试使用R中的sourceCpp进行编译时出错

时间:2014-02-06 16:21:29

标签: c++ r rcpp

我是C ++的新手和Rccp包,但是我在rcpp的库中找到了一些代码,可以让你从多元正态分布中生成。代码是

#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;

// [[Rcpp::export]]
arma::mat mvrnormArma(int n, arma::vec mu, arma::mat sigma) {
   int ncols = sigma.n_cols;
   arma::mat Y = arma::randn(n, ncols);
   return arma::repmat(mu, 1, n).t() + Y * arma::chol(sigma);
}

将其保存在文件multivgaussian.cpp中,当我尝试使用

在R中编译它时
sourceCpp("multivgaussian.cpp")

我收到此错误消息

Error in dyn.load("/tmp/RtmpGoFAwi/sourcecpp_6a751b6a3bee/sourceCpp_67198.so") : 
  unable to load shared object '/tmp/RtmpGoFAwi/sourcecpp_6a751b6a3bee/sourceCpp_67198.so':
  /tmp/RtmpGoFAwi/sourcecpp_6a751b6a3bee/sourceCpp_67198.so: undefined symbol: _ZN4Rcpp8internal14r_vector_startILi13EEEPNS_6traits12storage_typeIXT_EE4typeEP7SEXPREC

如果我尝试编译它,我也试着看看会发生什么(在终端中)。

R CMD SHLIB multivgaussian.cpp
g++ -I/maths/R/lib64/R/include -DNDEBUG  -I/usr/local/include    -fpic  -g -O2  -c multivgaussian.cpp -o multivgaussian.o
multivgaussian.cpp:1:27: error: RcppArmadillo.h: No such file or directory
multivgaussian.cpp:4: error: ‘Rcpp’ is not a namespace-name
multivgaussian.cpp:4: error: expected namespace-name before ‘;’ token
multivgaussian.cpp:7: error: ‘arma’ has not been declared
multivgaussian.cpp:7: error: expected constructor, destructor, or type conversion before ‘mvrnormArma’
make: *** [multivgaussian.o] Error 1

这可能很简单但我在网上找不到任何东西。非常感谢, 的Charis

2 个答案:

答案 0 :(得分:1)

Rcpp本周发布了一个版本,要求重建用户包。确保您的RcppArmadillo也重建。

在你的R CMD SHLIB示例中,你没有告诉R关于RcppArmadillo依赖关系,所以它无法正常工作。在第一个例子中的行

// [[Rcpp::depends(RcppArmadillo)]]

负责这一点,但您仍然遇到链接器问题 - 可能是由于版本不匹配。

答案 1 :(得分:0)

我最近在编译一个包含多个 for 循环的简单 Rcpp 函数时遇到了同样的错误。虽然代码在 Rstudio 中运行成功,但在 lsf 网格上运行时遇到了相同的 dyn.loadunable to load shared object 错误。

我所做的是指定一个缓存目录来使用 cacheDirsourceCpp 中的 cppFunction 选项编译函数。对我来说也很有效。

相关问题