【问题标题】:Error compiling R package samr: `ld: library not found for -lgfortran`编译 R 包 samr 时出错:`ld: library not found for -lgfortran`
【发布时间】:2023-04-13 08:39:02
【问题描述】:

我正在尝试install.packages("samr"),但它看起来像是在尝试使用 Fortran 编译文件,但我的 Fortran 似乎不在安装程序指定的路径上,我的 Fortran 来自 brew install gcc(我相信是正确的路径为/usr/local/Cellar/gcc/7.1.0/lib/gcc/7)。这是我的错误信息:

install.packages("samr")
--- Please select a CRAN mirror for use in this session ---
Package which is only available in source form, and may need
  compilation of C/C++/Fortran: ‘samr’
Do you want to attempt to install these from sources?
y/n: y
installing the source package ‘samr’

trying URL 'https://cloud.r-project.org/src/contrib/samr_2.0.tar.gz'
Content type 'application/x-gzip' length 36702 bytes (35 KB)
==================================================
downloaded 35 KB

* installing *source* package ‘samr’ ...
** libs
gfortran   -fPIC  -g -O2  -c rankcol.f -o rankcol.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o samr.so rankcol.o -L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin15/6.1.0'
ld: warning: directory not found for option '-L/usr/local/gfortran/lib'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [samr.so] Error 1
ERROR: compilation failed for package ‘samr’
* removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/samr’

The downloaded source packages are in
    ‘/private/var/folders/v_/n5nqr5812074ct0zkldqhg500000gn/T/RtmpBn8P5n/downloaded_packages’
Warning message:
In install.packages("samr") :
  installation of package ‘samr’ had non-zero exit status

【问题讨论】:

    标签: r macos gcc fortran gfortran


    【解决方案1】:

    看起来像是环境特定的问题。我每天都在使用 R 3.3.3,并且我能够毫无问题地安装该软件包:

    # this one is required by samr and is no longer available via CRAN
    source("https://bioconductor.org/biocLite.R")
    biocLite("impute")
    # samr, itself, can be installed from CRAN
    install.packages("samr")
    library(samr)
    

    但是,我使用的 gfortran 安装略有不同 - 直接来自源代码:

    https://gcc.gnu.org/wiki/GFortranBinaries

    gfortran --version
    GNU Fortran (GCC) 6.3.0
    Copyright (C) 2016 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

    这是从项目页面安装 gfortran 时的位置

    > which gfortran
    /usr/local/bin/gfortran
    > ls -l /usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0/ | wc -l
          20
    

    也许您可以尝试从项目页面安装它?不幸的是,对于 macOS,R 包(尤其是 Java 和 Fortran)中有很多强假设。

    【讨论】:

    • 感谢您的回答,我基本上做了同样的事情,并且成功了。在此之前,我还尝试直接从他们的页面下载并安装它,但一直遇到版本问题。但是按照 gcc.gnu.org 中的说明安装 gfortran 似乎是解决方案。
    • .oOo。凉爽的!享受 Fortran 的乐趣! ;) .oOo.