clang-7: 错误:链接器命令在macOS Big Sur上以退出码1失败。

20
在 Big Sur 之后的 macOS 上安装需要编译的 R 包存在问题。以下是一个这样的例子。
> install.packages('nlme')

  There is a binary version available but the source version is later:
      binary  source needs_compilation
nlme 3.1-150 3.1-151              TRUE

Do you want to install from sources the package which needs compilation? (Yes/no/cancel) Yes
installing the source package ‘nlme’

trying URL 'https://cran.rstudio.com/src/contrib/nlme_3.1-151.tar.gz'
Content type 'application/x-gzip' length 805592 bytes (786 KB)
==================================================
downloaded 786 KB

* installing *source* package ‘nlme’ ...
** package ‘nlme’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
"gfortran-4.8" -fno-optimize-sibling-calls  -fPIC  -Wall -g -O2  -c chol.f -o chol.o
gfortran-4.8: warning: couldn’t understand kern.osversion ‘20.1.0
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c corStruct.c -o corStruct.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c gnls.c -o gnls.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c init.c -o init.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c matrix.c -o matrix.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c nlOptimizer.c -o nlOptimizer.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c nlme.c -o nlme.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c nlmefit.c -o nlmefit.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c pdMat.c -o pdMat.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I/usr/local/include   -fPIC  -Wall -g -O2  -c pythag.c -o pythag.o
"gfortran-4.8" -fno-optimize-sibling-calls  -fPIC  -Wall -g -O2  -c rs.f -o rs.o
gfortran-4.8: warning: couldn’t understand kern.osversion ‘20.1.0
clang -mmacosx-version-min=10.13 -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 nlme.so chol.o corStruct.o gnls.o init.o matrix.o nlOptimizer.o nlme.o nlmefit.o pdMat.o pythag.o rs.o  -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: framework not found CoreFoundation
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nlme.so] Error 1
ERROR: compilation failed for package ‘nlme’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/nlme’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/nlme’
Warning in install.packages :
  installation of package ‘nlme’ had non-zero exit status

The downloaded source packages are in
    ‘/private/var/folders/_3/b8whcf8d1bb8w_lr2rrscb5m0000gp/T/RtmpWCjwfs/downloaded_packages’

我尝试重新安装Xcode和命令行工具,并完成了Brew的更新和升级。欢迎提出任何建议。


刚刚发现了这个(被忽略的)问题,看起来相关 https://stackoverflow.com/questions/65047685/r-package-installation-fail-ld-framework-not-found-corefoundation-clang-7-err - geotheory
2
许多其他人遇到了相同的问题。https://pbs.twimg.com/media/EbLLgorUcAEpWYf?format=png&name=900x900 - IRTFM
感谢@IRTFM的帮助。虽然很令人沮丧! - geotheory
最好与R-MAC-sig邮件列表进行沟通。 - IRTFM
1
我卡在同样的问题上。我尝试了r-macos-rtools,但它在Big Sur上安装不起来。这里有一些关于如何修复安装的信息,尽管我自己没能成功:https://github.com/rmacoslib/r-macos-rtools/issues/42 - epsilone
2个回答

37
这是一个具有挑战性的过程,但以下是我在MacOS Big Sur上编译R软件包的步骤:
  1. 重新安装Xcode命令行工具
(不要相信软件更新,即使它说“已经是最新版本” - 它撒谎了 - brew doctor 命令显示我的版本实际上是旧的)
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install

通过Homebrew(安装Homebrew的说明)安装gcc和llvm,如果你已经安装了gcc/llvm,请跳到下一步。
# WARNING: This can take several hours
brew install gcc
brew install llvm

如果您已经通过Homebrew安装了gcc和llvm:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
brew reinstall llvm

将一些头文件链接到 /usr/local/include。
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/

# I believe you can safely ignore warnings like this:
#ln: /usr/local/include//tcl.h: File exists
#ln: /usr/local/include//tclDecls.h: File exists
#ln: /usr/local/include//tclPlatDecls.h: File exists
#ln: /usr/local/include//tclTomMath.h: File exists
#ln: /usr/local/include//tclTomMathDecls.h: File exists
#ln: /usr/local/include//tk.h: File exists
#ln: /usr/local/include//tkDecls.h: File exists
#ln: /usr/local/include//tkPlatDecls.h: File exists

编辑你的~/.R/Makevars文件,只包含以下几行内容:
LOC=/usr/local/gfortran
CC=$(LOC)/bin/gcc -fopenmp
CXX=$(LOC)/bin/g++ -fopenmp
CXX11 = $(LOC)/bin/g++ -fopenmp

CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib,-L/usr/local/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/include

FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CXX1X=/usr/local/gfortran/bin/g++
CXX98=/usr/local/gfortran/bin/g++
CXX11=/usr/local/gfortran/bin/g++
CXX14=/usr/local/gfortran/bin/g++
CXX17=/usr/local/gfortran/bin/g++

从源代码中编译一个R/Rstudio的软件包。
# Compile the nlme package from source
install.packages("nlme", type = "source")

# To check whether openmp is enabled, compile data.table:
install.packages("data.table", type = "source")

2023年9月编辑

更新macOS到Ventura似乎又破坏了编译器工具链。尝试从源代码构建软件包时出现了以下错误:

ld: unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '/usr/local/gfortran/lib/libgcc_ext.10.5.dylib'
collect2: error: ld returned 1 exit status
make: *** [data.table.so] Error 1
Call `pkgbuild::check_build_tools(debug = TRUE)` to diagnose the problem.

@obruzzi在下面的回答中建议删除新的SDK(这样默认会使用v13.3的SDK),但一般来说,这不是一个好的做法,因为你希望使用最新的工具,以便它们能够“正确地协同工作”。
我建议的解决方案是重新安装命令行工具和gcc(按照上述步骤1、2和3进行操作),然后编辑你的~/.R/Makevars文件为:
LOC=/usr/local/Cellar/gcc/13.2.0
CC=$(LOC)/bin/gcc-13 -fopenmp
CXX=$(LOC)/bin/g++-13 -fopenmp
CXX11=$(LOC)/bin/g++-13 -fopenmp

CFLAGS=-g -O3 -Wall -std=gnu99 -mtune=native -pipe
CXXFLAGS=-g -O3 -Wall -std=c++11 -mtune=native -pipe
LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib
CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/local/include

# Previous vars
FLIBS=-L$(LOC)/lib -L/usr/local/Cellar/gcc/13.2.0/lib/gcc/current/ -lgfortran -lquadmath -lm
CXX1X=$(LOC)/bin/g++-13 -fopenmp
CXX98=$(LOC)/bin/g++-13 -fopenmp
CXX11=$(LOC)/bin/g++-13 -fopenmp
CXX14=$(LOC)/bin/g++-13 -fopenmp
CXX17=$(LOC)/bin/g++-13 -fopenmp

然后,运行pkgbuild::check_build_tools(debug = TRUE)返回您的系统已准备好构建软件包!并且data.table按预期编译并支持openMP。

1
谢谢Jared,我运行了 $ git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core" fetch --unshallow 命令,但是出现了 fatal: --unshallow on a complete repository does not make sense 的错误提示。你有什么想法吗? - geotheory
1
安装完成,您的工作流程也就修复了。非常感谢 :) - geotheory
4
经过所有步骤和重启后,我遇到了“ld: framework not found CoreFoundation”的问题。对于我来说,安装适用于R/Mac的新版gfortran解决了这个问题:https://github.com/fxcoudert/gfortran-for-macOS/releases/tag/10.2-bigsur-intel - speleo
1
@speleo,就是这样!从那个链接安装gfortran解决了我的问题。一开始有点犹豫,因为仓库只是来自一个研究人员,但它让我的R再次工作了,非常感谢! - Florag
1
实际上,有一种替代方法可以安装gfortran链接。那就是使用brew提供的gcc版本的gfortran,但我认为这需要与上面提供的不同的makevars行。请参见此链接:https://github.com/Homebrew/homebrew-cask/issues/96135 - Florag
显示剩余9条评论

1

MacOS Ventura 13.5
找到并尝试了许多不同的解决方案,包括上面提到的那个。对我来说,解决办法是从终端中删除有问题的目录。

cd /Library/Developer/CommandLineTools/SDKs
rm -rf MacOSX.sdk

如果您不确定的话,可以将目录重命名,而不是删除它。
cd /Library/Developer/CommandLineTools/SDKs
sudo mv MacOSX.sdk MacOSX_old.sdk

1
谢谢 @obruzzi!我刚刚遇到了这个问题。你的方法很好(+1),它可以解决问题,但我怀疑修复只是“暂时的”,即在下一次 macOS 更新之前。我认为从长远来看,使用新的 SDK 和最新的 GCC 会更好 - 我已经编辑了我的帖子,展示了如何做到这一点(尽管,如果你的方法有效,最好不要去碰它,直到它再次出问题)。感谢你发布解决方案! - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接