Rcpp 警告:"directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran'"

Rcpp warning: "directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran'"

这个问题与其他一些问题有关,例如 RccpArmadillo or element-wise-multiplication

但是,我的设置使我不知道我必须 edit/simlink 做什么才能使 Rccp 运行 没有给我警告。

我正在 Mac 10.9 (mavericks) 使用最新的 R 版本。

一开始,尝试从这里 RccpvsR 获得的以下代码时,出现错误:

ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)`  

然后,基于 RccpArmadillo 我做了以下操作:

# Update FLIBS in ~/.R/Makevars
FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran
#Re-Install from source
install.packages(c("Rcpp","RcppArmadillo","inline"),type="source")
#Restart R

这只是尝试,因为我没有 /usr/local/Cellar/gfortran/ 目录。事实上,我所有的 libgfortran* 文件都在这里(在 macports 目录):

>ls /opt/local/lib/gcc48/libgfortran.*
/opt/local/lib/gcc48/libgfortran.3.dylib       /opt/local/lib/gcc48/libgfortran.dylib
/opt/local/lib/gcc48/libgfortran.a       /opt/local/lib/gcc48/libgfortran.spec

这里 /opt/local/lib/gcc48/gcc/x86_64-apple-darwin13/4.8.3/libgfortranbegin.a 并且我在任何地方都没有 gfortran 文件。

然后我再次尝试代码 RccpvsR,令人惊讶的是,它成功了!。除了我收到警告之外:

ld: warning: directory not found for option '-L/usr/local/Cellar/gfortran/4.8.2/gfortran' 

当然,它不存在,但是由该代码创建的函数,cosineRcpp,运行没有问题。

因此,所有这些,问问是否有人知道我是否必须将 /opt/local/lib/gcc48/ 处的 libgfortran 文件模拟链接为:

ln -s /opt/local/lib/gcc48/libgfortran.* /usr/local/lib/

然后是 remove/edit 行:

FLIBS=-L/usr/local/Cellar/gfortran/4.8.2/gfortran

~/.R/Makevars

或者如果我必须安装新的东西。

提前感谢您的宝贵时间!

简答

只需将 libgfortran 的路径放入 FLIBS,例如

FLIBS=-L/opt/local/lib/gcc48/

或者,将其中的文件符号链接到 /usr/local/lib/,如果您愿意的话。但是,此解决方案非常脆弱,因为如果您更新 gfortran 或将其移动到其他目录,很容易忘记更新此路径。

稍微长一点的答案

您可以在 gfortran 中查询到 libgfortran.dylib 的路径,例如

gfortran -print-file-name=libgfortran.dylib

您可以直接在 Makevars 文件中执行此操作;例如

FLIBS = -L`gfortran -print-file-name=libgfortran.dylib | xargs dirname`

过时的长答案

尝试直接从 gfortran 输出解析适当的 FLIBS

首先,一些背景。 /usr/local/Cellar 目录是 homebrew 使用的默认路径,它是 OS X 的包管理器。可以将其视为 macports.

的替代方案

Homebrew 现在提供 gfortran 及其相关库作为 gcc 包的一部分,因此它安装 FORTRAN 库的路径现在已经改变。然而,这些可以(通常)使用 gfortran -print-search-dirs 来发现。例如,在我的系统上,

gfortran -print-search-dirs

会给我

install: /usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/
programs: =/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/
libraries: =/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../x86_64-apple-darwin14.0.0/4.9.2/:/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../:/lib/x86_64-apple-darwin14.0.0/4.9.2/:/lib/:/usr/lib/x86_64-apple-darwin14.0.0/4.9.2/:/usr/lib/

拆分,用R打印,我看到:

[[1]]
[1] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/"

[[2]]
[1] "/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                    
[2] "/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                    
[3] "/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin14.0.0/"                                                                                          
[4] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                
[5] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/"                                                                                      
[6] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/x86_64-apple-darwin14.0.0/4.9.2/"
[7] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/bin/"                                

[[3]]
[1] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                
[2] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/x86_64-apple-darwin14.0.0/4.9.2/"
[3] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/"                                
[4] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../x86_64-apple-darwin14.0.0/4.9.2/"                                       
[5] "/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../"                                                                       
[6] "/lib/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                                                         
[7] "/lib/"                                                                                                                                                         
[8] "/usr/lib/x86_64-apple-darwin14.0.0/4.9.2/"                                                                                                                     
[9] "/usr/lib/"  

就我而言,libgfortran 实际上住在这里:

/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../

这就是我们要传递给 FLIBS 的路径。但是,把它拉出来有点痛苦,所以让我们告诉 FLIBS 使用 gfortran 通常使用的任何路径:

gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||'

很好,但我们希望库路径采用适合编译器的格式;即,带有 -L 前缀。让我们用 sed:

gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'

此输出(为了便于阅读而拆分)

-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../../../../x86_64-apple-darwin14.0.0/lib/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9/gcc/x86_64-apple-darwin14.0.0/4.9.2/../../../
-L/lib/x86_64-apple-darwin14.0.0/4.9.2/
-L/lib/
-L/usr/lib/x86_64-apple-darwin14.0.0/4.9.2/
-L/usr/lib/

总而言之,这意味着以下内容应该适合您,至少在 OS X 上,但应该(通常)在具有 gfortran 的任何平台上工作(只要它在PATH):

FLIBS=`gfortran -print-search-dirs | grep ^libraries: | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'`

这并不完美,例如如果你的路径中有空格,它将失败——如果你这样做,1) 你应得的和 2) 它也应该是一个 'relatively' 简单的修复。

对@kevin-ushey 的答案(至少我需要;我使用 Prezto)的一个小调整是在 grep '^libraries:' 中添加单引号,导致:

FLIBS=`gfortran -print-search-dirs | grep '^libraries:' | sed 's|libraries: =||' | sed 's|:| -L|g' | sed 's|^|-L|'`

将该行添加到我的 ~/.R/Makevars 文件中就成功了!