问题描述
我对 C++ 还很陌生,但在 R 上花了很多时间.我正在尝试使用 RcppArmadillo,其中 .cpp 文件是使用 sourceCpp 函数获取的.我的示例代码来自
I'm pretty new to C++, but have spent a lot of time with R. I'm trying to use RcppArmadillo, where the .cpp file is sourced in using the sourceCpp function. My example code is from
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-June/006150.html
并显示如下:
#include <RcppArmadillo.h>
using namespace Rcpp ;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::colvec rowSumsRcppArmadillo(NumericMatrix x){
arma::mat X = arma::mat(x.begin(), x.nrow(), x.ncol(), false);
return arma::sum(X, 1);
}
我已经安装了 Rcpp 和 RcppArmadillo 包,并且已经成功地使用 Rcpp(没有 RcppArmadillo)来集成 C++ 函数.但是,对于 RcppArmadillo,我收到以下错误:
I have the Rcpp and RcppArmadillo packages installed, and have successfully used Rcpp (without RcppArmadillo) to integrate C++ functions. However, for RcppArmadillo, I am getting the following error:
> sourceCpp("rowSums.cpp")
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple- darwin8/4.2.3/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_76327.so] Error 1
...
Error in sourceCpp("rowSums.cpp") :
Error 1 occurred building shared library.
有什么想法吗?谢谢.
推荐答案
查看错误:ld: library not found for -lgfortran
您需要安装 Fortran 库,因为 RcppArmadillo 在 src/Makevars
中有这个:
You need to install the Fortran libraries as RcppArmadillo has this in src/Makevars
:
PKG_LIBS=`$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` \
$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
您可以通过 R 获得 Lapack 和 Blas,但需要 Fortran 库.有关从何处获取此信息,请参阅 R 的 OS X 文档;我相信这个地方是 Simon 在 AT&T 的页面,但我自己不是 OS X 用户.如果有疑问,请在 r-sig-mac 上询问,那里还有许多关于升级到 OS X 10.9 时发生的各种损坏的讨论主题.
You get Lapack and Blas via R, but need the Fortran libs. See the OS X documentation for R about where to get this; I believe the place is Simon's page at AT&T but I am not an OS X user myself. If in doubt, ask on r-sig-mac where there are also numerous discussion threads concerning the various breakages which occurred with upgrades to OS X 10.9.
这篇关于使用 sourceCpp() 在 Rcpp 和 RcppArmadillo 中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!