macOS更新后无法从源码安装R包的问题求助
R包源码安装失败(gfortran链接错误)
问题背景
已安装Xcode命令行工具,按官方指引安装了gfortran,取消了RStudio包设置里的唯一HTTPS勾选,但安装estimatr等需要源码编译的R包时仍报错,核心问题是链接器找不到gfortran库。
错误日志
22 warnings generated. clang++ -arch arm64 -std=gnu++11 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o estimatr.so RcppExports.o horvitz_thompson_variance.o lm_robust_helper.o naomit.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lm -F/Library/Frameworks/R/framework/.. -framework R -Wl,-framework -Wl,CoreFoundation ld: warning: -single_module is obsolete ld: warning: -multiply_defined is obsolete ld: warning: search path '/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0' not found ld: warning: search path '/opt/R/arm64/gfortran/lib' not found ld: library 'gfortran' not found clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [estimatr.so] Error 1 ERROR: compilation failed for package ‘estimatr’ * removing ‘/Users/myname/Library/R/arm64/4.1/library/estimatr’
Session信息
R version 4.1.1 (2021-08-10) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS 14.0 Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] devtools_2.4.2 usethis_2.2.2 loaded via a namespace (and not attached): [1] prettyunits_1.2.0 ps_1.7.5 crayon_1.5.2 R6_2.5.1 lifecycle_1.0.3 magrittr_2.0.3 [7] rlang_1.1.1 cachem_1.0.6 cli_3.6.1 remotes_2.4.2.1 rstudioapi_0.13 fs_1.6.3 [13] callr_3.7.3 vctrs_0.6.4 ellipsis_0.3.2 tools_4.1.1 glue_1.6.2 purrr_1.0.2 [19] pkgload_1.3.3 processx_3.8.2 fastmap_1.1.0 compiler_4.1.1 pkgbuild_1.4.2 sessioninfo_1.2.2 [25] memoise_2.0.1
可尝试的解决方法
匹配gfortran安装路径
打开终端执行which gfortran查看实际安装路径,如果不是R期望的/opt/R/arm64/gfortran/bin/gfortran,手动创建软链接:sudo mkdir -p /opt/R/arm64/gfortran/lib sudo ln -s /usr/local/gfortran/lib/* /opt/R/arm64/gfortran/lib/ sudo ln -s /usr/local/gfortran/lib/gcc/aarch64-apple-darwin20/11.0.0 /opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0注:如果你的gfortran安装在其他路径,替换上述命令中的
/usr/local/gfortran为实际路径。设置R编译环境变量
终端执行以下命令临时配置链接路径:export LDFLAGS="-L/usr/local/gfortran/lib -lgfortran" export CPPFLAGS="-I/usr/local/gfortran/include"重启R/RStudio后再尝试安装包。若要永久生效,将这两行添加到
~/.zshrc或~/.bash_profile(取决于你使用的shell)。升级R版本
当前使用的R 4.1.1版本较旧,与macOS 14.0兼容性可能存在问题,建议升级到最新的ARM版R,新版本对Apple Silicon的支持更完善,可能自动解决路径匹配问题。优先安装二进制包
在R中执行以下命令,跳过源码编译直接安装预编译包:install.packages("estimatr", type = "binary")重装Xcode命令行工具
执行xcode-select --install重新安装命令行工具,确保clang等编译工具完整且为最新版本。
内容的提问来源于stack exchange,提问作者canIchangethis
相关产品推荐
相关产品推荐

