Rcpp 错误:/usr/bin/ld 找不到 -lgfortran [英] Rcpp error: /usr/bin/ld cannot find -lgfortran

查看:37
本文介绍了Rcpp 错误:/usr/bin/ld 找不到 -lgfortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Seamless R and C++ Integration with Rcpp"一书.我在 Ubuntu 12.04 上使用 R 版本 3.1.0.我无法弄清楚如何正确链接必要的库.我在 R 中有以下代码:

I am working through the book "Seamless R and C++ Integration with Rcpp". I am using R version 3.1.0 on Ubuntu 12.04. I cannot figure out how to properly link the necessary libraries. I have the following code in 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 

根据对类似问题的回复,

Based on this response to a similar question,

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

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

it would appear that I simply need to install the FORTRAN compiler. However, I have installed gfortran using the Ubuntu package manager and still receive the same error. From terminal:

$ 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() 函数.非常感谢任何建议.

I have also been unsuccessful trying to use the CxxFlags() and LdFlags() functions. Any suggestions are greatly appreciated.

运行 sourceCpp() 会产生以下结果:

Running sourceCpp() produces the following:

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 命令给出:

Running the dpkg command gives:

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

您的机器需要 libgfortrain-$VERSION-dev 包.在某些时候,gfortran 通过 r-base-dev 及其对 build-essentials 的依赖暗示了这一点.

You need the libgfortrain-$VERSION-dev package for your machine. At some point having gfortran implied this via r-base-dev and its dependence on build-essentials.

版本号在您的 12.04 版本中当然会有所不同;这是来自运行 13.10 的机器.

Version numbers will of course be different on your 12.04 release; this was from a machine running 13.10.

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

Edit 2, based on your update: Your use of sourceCpp() is incorrect. You are not telling Rcpp that you need Armadillo, so Rcpp responds by saying it does not know Armadillo. You can either use a Rcpp::depends() in the cpp file, or use the plugin= argument.

所以这就是我将如何编写您上面的代码.然后,您只需在文件上调用 sourceCpp() 即可创建一个您可以调用的函数 rcppSim():

So here is how I would write the code you have above. You can then just call sourceCpp() on the file which will create a function rcppSim() you can call:

#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 错误:/usr/bin/ld 找不到 -lgfortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆