0
我是C++和包Rccp的新手,但我在rcpp的庫中找到了一些代碼,可以從多元正態分佈中生成代碼。該代碼是嘗試使用R中的sourceCpp進行編譯時出錯
#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,當我嘗試用
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中看看會發生什麼(在終端中),如果我試圖編譯它。
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
感謝您的快速回復(@suspectus,@DirkEddelbuettel)我剛剛發現了Rcpp的新版本,我必須更新R的版本才能安裝新的軟件包。我想這就是它。我必須等到明天大學纔會更新我的R版。再次感謝。 –
你不需要。如果你有一致的版本,那麼它應該工作。只是不更新RcppArmadillo和Rcpp之一,但不更新其中之一。 –
剛剛做到了。現在一切似乎都很好。謝謝。 –