简体   繁体   English

使用 sourceCpp() 在 Rcpp 和 RcppArmadillo 中出错

[英]Error in R with Rcpp and RcppArmadillo using 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.我对 C++ 还很陌生,但在 R 上花了很多时间。我正在尝试使用 RcppArmadillo,其中 .cpp 文件是使用 sourceCpp 函数获取的。 My example code is from我的示例代码来自

http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-June/006150.html http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2013-June/006150.html

and displayed below:并显示如下:

#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);
}

I have the Rcpp and RcppArmadillo packages installed, and have successfully used Rcpp (without RcppArmadillo) to integrate C++ functions.我已经安装了 Rcpp 和 RcppArmadillo 包,并且已经成功地使用 Rcpp(没有 RcppArmadillo)来集成 C++ 函数。 However, for RcppArmadillo, I am getting the following error:但是,对于 RcppArmadillo,我收到以下错误:

> 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.

Any ideas?有任何想法吗? Thanks.谢谢。

See the error: ld: library not found for -lgfortran查看错误: ld: library not found for -lgfortran

You need to install the Fortran libraries as RcppArmadillo has this in src/Makevars :您需要安装 Fortran 库,因为 RcppArmadillo 在src/Makevars有这个:

PKG_LIBS=`$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` \
          $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

You get Lapack and Blas via R, but need the Fortran libs.您可以通过 R 获得 Lapack 和 Blas,但需要 Fortran 库。 See the OS X documentation for R about where to get this;有关从何处获取此信息,请参阅 R 的 OS X 文档; I believe the place is Simon's page at AT&T but I am not an OS X user myself.我相信这个地方是西蒙在 AT&T 的页面,但我自己不是 OS X 用户。 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.如果有疑问,请在 r-sig-mac 上询问,那里还有许多关于升级到 OS X 10.9 时发生的各种损坏的讨论主题。

I have encountered the same problem with you.我和你遇到了同样的问题。 I just go to http://cran.r-project.org/bin/macosx/tools/ and install gfortran-4.2.3.pkg on my computer and then use sourceCpp, it works then.我只是去 http://cran.r-project.org/bin/macosx/tools/ 并 在我的电脑上 安装 gfortran-4.2.3.pkg 然后使用 sourceCpp,它就可以工作了。 You can try this, which is the simplest way I have found right now.你可以试试这个,这是我现在找到的最简单的方法。

The more current page to consult is at https://mac.r-project.org/tools/要查阅的最新页面位于https://mac.r-project.org/tools/

In case there are other folks like me having similar trouble with 'build' and 'check' with Armadillo projects after you've install one of the recommended fortran libraries for Mac, you need to follow [the instructions here] 1 : https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/如果在您为 Mac 安装了推荐的 Fortran 库之一后,还有像我这样的其他人在使用 Armadillo 项目进行“构建”和“检查”时遇到类似问题,您需要遵循 [此处的说明] 1https:/ /thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/

I was trying to change the LDFLAGS environment variable in the shell, but it wasn't passing through.我试图在 shell 中更改 LDFLAGS 环境变量,但它没有通过。 However, setting it correctly in ~/.R/Makevars seemed to work.但是,在 ~/.R/Makevars 中正确设置它似乎有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM