Linker Rcpp错误:/usr/bin/ld找不到-lgfortran 我正在通过《无缝R和C++与RCPP集成》这本书进行研究。我在Ubuntu 12.04上使用的是R版本3.1.0。我不知道如何正确链接必要的库。我在R中有以下代码: R> library(Rcpp) R> library(RcppArmadillo) R> suppressMessages(require(inline)) R> code <- ' + arma::mat coeff = Rcpp::as<arma::mat>(a); + arma::mat errors = Rcpp::as<arma::mat>(u); + int m = errors.n_rows; + int n = errors.n_cols; + arma::mat simdata(m,n); + simdata.row(0) = arma::zeros<arma::mat>(1, n); + for (int row=1; row < m; row++) { + simdata.row(row) = simdata.row(row-1)*trans(coeff) + + errors.row(row); + } + return Rcpp::wrap(simdata); + ' R> rcppSim <- cxxfunction(signature(a="numeric", u="numeric"), + code, plugin="RcppArmadillo") /usr/bin/ld: cannot find -lgfortran collect2: error: ld returned 1 exit status make: *** [file167d1a7cd1ad.so] Error 1 ERROR(s) during compilation: source code errors or compiler configuration errors! Program source: 1: 2: // includes from the plugin 3: #include <RcppArmadillo.h> 4: #include <Rcpp.h> 5: 6: 7: #ifndef BEGIN_RCPP 8: #define BEGIN_RCPP 9: #endif 10: 11: #ifndef END_RCPP 12: #define END_RCPP 13: #endif 14: 15: using namespace Rcpp; 16: 17: 18: // user includes 19: 20: 21: // declarations 22: extern "C" { 23: SEXP file167d1a7cd1ad( SEXP a, SEXP u) ; 24: } 25: 26: // definition 27: 28: SEXP file167d1a7cd1ad( SEXP a, SEXP u ){ 29: BEGIN_RCPP 30: 31: arma::mat coeff = Rcpp::as<arma::mat>(a); 32: arma::mat errors = Rcpp::as<arma::mat>(u); 33: int m = errors.n_rows; 34: int n = errors.n_cols; 35: arma::mat simdata(m,n); 36: simdata.row(0) = arma::zeros<arma::mat>(1, n); 37: for (int row=1; row < m; row++) { 38: simdata.row(row) = simdata.row(row-1)*trans(coeff) 39: + errors.row(row); 40: } 41: return Rcpp::wrap(simdata); 42: 43: END_RCPP 44: } 45: 46: Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! /usr/bin/ld: cannot find -lgfortran collect2: error: ld returned 1 exit status make: *** [file167d1a7cd1ad.so] Error 1 Calls: cxxfunction -> compileCode In addition: Warning message: running command '/usr/lib/R/bin/R CMD SHLIB file167d1a7cd1ad.cpp 2> file167d1a7cd1ad.cpp.err.txt' had status 1

Linker Rcpp错误:/usr/bin/ld找不到-lgfortran 我正在通过《无缝R和C++与RCPP集成》这本书进行研究。我在Ubuntu 12.04上使用的是R版本3.1.0。我不知道如何正确链接必要的库。我在R中有以下代码: R> library(Rcpp) R> library(RcppArmadillo) R> suppressMessages(require(inline)) R> code <- ' + arma::mat coeff = Rcpp::as<arma::mat>(a); + arma::mat errors = Rcpp::as<arma::mat>(u); + int m = errors.n_rows; + int n = errors.n_cols; + arma::mat simdata(m,n); + simdata.row(0) = arma::zeros<arma::mat>(1, n); + for (int row=1; row < m; row++) { + simdata.row(row) = simdata.row(row-1)*trans(coeff) + + errors.row(row); + } + return Rcpp::wrap(simdata); + ' R> rcppSim <- cxxfunction(signature(a="numeric", u="numeric"), + code, plugin="RcppArmadillo") /usr/bin/ld: cannot find -lgfortran collect2: error: ld returned 1 exit status make: *** [file167d1a7cd1ad.so] Error 1 ERROR(s) during compilation: source code errors or compiler configuration errors! Program source: 1: 2: // includes from the plugin 3: #include <RcppArmadillo.h> 4: #include <Rcpp.h> 5: 6: 7: #ifndef BEGIN_RCPP 8: #define BEGIN_RCPP 9: #endif 10: 11: #ifndef END_RCPP 12: #define END_RCPP 13: #endif 14: 15: using namespace Rcpp; 16: 17: 18: // user includes 19: 20: 21: // declarations 22: extern "C" { 23: SEXP file167d1a7cd1ad( SEXP a, SEXP u) ; 24: } 25: 26: // definition 27: 28: SEXP file167d1a7cd1ad( SEXP a, SEXP u ){ 29: BEGIN_RCPP 30: 31: arma::mat coeff = Rcpp::as<arma::mat>(a); 32: arma::mat errors = Rcpp::as<arma::mat>(u); 33: int m = errors.n_rows; 34: int n = errors.n_cols; 35: arma::mat simdata(m,n); 36: simdata.row(0) = arma::zeros<arma::mat>(1, n); 37: for (int row=1; row < m; row++) { 38: simdata.row(row) = simdata.row(row-1)*trans(coeff) 39: + errors.row(row); 40: } 41: return Rcpp::wrap(simdata); 42: 43: END_RCPP 44: } 45: 46: Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! /usr/bin/ld: cannot find -lgfortran collect2: error: ld returned 1 exit status make: *** [file167d1a7cd1ad.so] Error 1 Calls: cxxfunction -> compileCode In addition: Warning message: running command '/usr/lib/R/bin/R CMD SHLIB file167d1a7cd1ad.cpp 2> file167d1a7cd1ad.cpp.err.txt' had status 1,linker,gfortran,rcpp,armadillo,Linker,Gfortran,Rcpp,Armadillo,请运行以下命令: edd@max:~$ dpkg -l | grep libgfortran | cut -c-75 ii libgfortran-4.7-dev:amd64 4.7.3-7ubuntu3 ii libgfortran-4.8-dev:amd64 4.8.1-10ubuntu9 ii libgfortran3:amd64

请运行以下命令:

edd@max:~$ dpkg -l | grep libgfortran | cut -c-75
ii  libgfortran-4.7-dev:amd64                     4.7.3-7ubuntu3           
ii  libgfortran-4.8-dev:amd64                     4.8.1-10ubuntu9          
ii  libgfortran3:amd64                            4.8.1-10ubuntu9          
edd@max:~$ 
您的机器需要
libgfortrain-$VERSION dev
软件包。在某些情况下,通过
r-base-dev
及其对
构建要素的依赖,使用
gfortran
意味着这一点

编辑:12.04版本的版本号当然会有所不同;这是一台运行13.10的机器发出的

编辑2,基于您的更新:您使用的
sourceCpp()
不正确。你没有告诉Rcpp你需要犰狳,所以Rcpp回应说它不认识犰狳。您可以在cpp文件中使用
Rcpp::depends()
,也可以使用
plugin=
参数

下面是我将如何编写上面的代码。然后,您只需调用文件上的
sourceCpp()
,该文件将创建一个函数
rcppSim()
,您可以调用:

#include <RcppArmadillo.h>

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

// [[Rcpp::export]]
arma::mat rcppSim(arma::mat coeff, arma::mat errors) {
   int m = errors.n_rows;
   int n = errors.n_cols;
   arma::mat simdata(m,n);
   simdata.row(0) = arma::zeros<arma::mat>(1, n);
   for (int row=1; row < m; row++) {
     simdata.row(row) = simdata.row(row-1)*trans(coeff) + errors.row(row);
   }
   return simdata;
}
#包括
//[[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::导出]]
arma::mat rcppSim(arma::mat系数,arma::mat误差){
int m=错误。n_行;
int n=错误。n_cols;
arma::mat simdata(m,n);
行(0)=arma::零(1,n);
对于(int行=1;行
您尝试过sourceCpp而不是CXX功能吗?非常感谢-我认为问题在于设置。我卸载了R,重新安装了R,并更新了我的所有软件包。重置PKG_cxflags和PKG_LIBS变量后,原始代码可以正常工作。顺便说一句,Rcpp是一个很棒的软件包。我相信它很快就会成为我最常用的软件包。太好了,很高兴知道它现在可以用了。您也可以随意访问rcpp-devel。我也遇到了同样的错误,最终,
~$sudo apt get install libgfortran-*
安装所有版本来处理它。您不需要这样做。请参阅我的一些其他答案,推荐
r-base-dev
,其中应包含正确的默认答案,您只需在其中添加即可。
~$ dpkg -l | grep libgfortran | cut -c-75
ii  libgfortran-4.7-dev                          4.7.3-2ubuntu1~12.04      
ii  libgfortran-4.8-dev                          4.8.1-2ubuntu1~12.04      
ii  libgfortran3                                 4.8.1-2ubuntu1~12.04      
~$ 
edd@max:~$ dpkg -l | grep libgfortran | cut -c-75
ii  libgfortran-4.7-dev:amd64                     4.7.3-7ubuntu3           
ii  libgfortran-4.8-dev:amd64                     4.8.1-10ubuntu9          
ii  libgfortran3:amd64                            4.8.1-10ubuntu9          
edd@max:~$ 
#include <RcppArmadillo.h>

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

// [[Rcpp::export]]
arma::mat rcppSim(arma::mat coeff, arma::mat errors) {
   int m = errors.n_rows;
   int n = errors.n_cols;
   arma::mat simdata(m,n);
   simdata.row(0) = arma::zeros<arma::mat>(1, n);
   for (int row=1; row < m; row++) {
     simdata.row(row) = simdata.row(row-1)*trans(coeff) + errors.row(row);
   }
   return simdata;
}