簡體   English   中英

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

[英]Error in R with Rcpp and RcppArmadillo using sourceCpp()

我對 C++ 還很陌生,但在 R 上花了很多時間。我正在嘗試使用 RcppArmadillo,其中 .cpp 文件是使用 sourceCpp 函數獲取的。 我的示例代碼來自

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,我收到以下錯誤:

> 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有這個:

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

您可以通過 R 獲得 Lapack 和 Blas,但需要 Fortran 庫。 有關從何處獲取此信息,請參閱 R 的 OS X 文檔; 我相信這個地方是西蒙在 AT&T 的頁面,但我自己不是 OS X 用戶。 如果有疑問,請在 r-sig-mac 上詢問,那里還有許多關於升級到 OS X 10.9 時發生的各種損壞的討論主題。

我和你遇到了同樣的問題。 我只是去 http://cran.r-project.org/bin/macosx/tools/ 並 在我的電腦上 安裝 gfortran-4.2.3.pkg 然后使用 sourceCpp,它就可以工作了。 你可以試試這個,這是我現在找到的最簡單的方法。

要查閱的最新頁面位於https://mac.r-project.org/tools/

如果在您為 Mac 安裝了推薦的 Fortran 庫之一后,還有像我這樣的其他人在使用 Armadillo 項目進行“構建”和“檢查”時遇到類似問題,您需要遵循 [此處的說明] 1https:/ /thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/

我試圖在 shell 中更改 LDFLAGS 環境變量,但它沒有通過。 但是,在 ~/.R/Makevars 中正確設置它似乎有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM