Rcpp错误:/ usr / bin / ld找不到-lgfortran

时间:2014-05-20 18:57:42

标签: linker gfortran rcpp armadillo

我正在阅读“使用Rcpp进行无缝R和C ++集成”一书。我在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 

基于对类似问题的回答,

http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2014-February/007245.html

看起来我只需要安装 FORTRAN编译器。但是,我已经使用Ubuntu包管理器安装了gfortran,但仍然收到相同的错误。从终端:

$ dpkg -s gfortran
Package: gfortran
Status: install ok installed
Priority: optional
Section: devel
Installed-Size: 33
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: i386
Source: gcc-defaults (1.112ubuntu5)
Version: 4:4.6.3-1ubuntu5
Provides: fortran-compiler
Depends: cpp (>= 4:4.6.3-1ubuntu5), gcc (>= 4:4.6.3-1ubuntu5), gfortran-4.6 
(>=   4.6.3-1~)
Suggests: gfortran-multilib, gfortran-doc
Description: GNU Fortran 95 compiler
This is the GNU Fortran 95 compiler, which compiles Fortran 95 on platforms
supported by the gcc compiler. It uses the gcc backend to generate optimized
code.

This is a dependency package providing the default GNU Fortran 95 compiler.
Original-Maintainer: Debian GCC Maintainers <debian-gcc@lists.debian.org>

我尝试使用CxxFlags()和LdFlags()函数也没有成功。非常感谢任何建议。

运行sourceCpp()会产生以下内容:

R> rcppSim <- sourceCpp("~/Dropbox/Rcpp/rcppSim.cpp")
rcppSim.cpp: In function ‘SEXPREC* file167d1a7cd1ad(SEXP, SEXP)’:
rcppSim.cpp:31:1: error: ‘arma’ has not been declared
rcppSim.cpp:31:11: error: expected ‘;’ before ‘coeff’
rcppSim.cpp:32:1: error: ‘arma’ has not been declared
rcppSim.cpp:32:11: error: expected ‘;’ before ‘errors’
rcppSim.cpp:33:9: error: ‘errors’ was not declared in this scope
rcppSim.cpp:35:1: error: ‘arma’ has not been declared
rcppSim.cpp:35:11: error: expected ‘;’ before ‘simdata’
rcppSim.cpp:36:1: error: ‘simdata’ was not declared in this scope
rcppSim.cpp:36:18: error: ‘arma’ has not been declared
rcppSim.cpp:36:30: error: ‘arma’ has not been declared
rcppSim.cpp:38:47: error: ‘coeff’ was not declared in this scope
rcppSim.cpp:38:52: error: ‘trans’ was not declared in this scope
make: *** [rcppSim.o] Error 1
g++ -I/usr/share/R/include -DNDEBUG    -I"/usr/lib/R/library/Rcpp/include"
-I"/usr/lib/R/library/RcppArmadillo/include" -I"/usr/lib/R/library/Rcpp/include"     
-fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security   
-Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c rcppSim.cpp -o rcppSim.o 
Error in sourceCpp("~/Dropbox/Rcpp/rcppSim.cpp") : 
Error 1 occurred building shared library.

运行dpkg命令会给出:

~$ 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      
~$ 

1 个答案:

答案 0 :(得分:1)

请运行以下命令:

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包。在某些时候,gfortran通过r-base-dev暗示了这一点及其对build-essentials的依赖。

编辑:版本号在您的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;
}