首页 > 解决方案 > clang-7:错误:链接器命令失败,macOS Big Sur 的退出代码为 1

问题描述

自 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 更新 + 升级。任何建议表示赞赏。

标签: rmacosclangmacos-big-sur

解决方案


这具有挑战性,但以下是我在 MacOS Big Sur 上从源代码编译 R 包的步骤:

  1. 重新安装 xcode 命令行工具

(不要相信软件更新,如果它说“最新” - 他撒谎 -brew doctor说我的版本实际上是旧的)

sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
  1. 通过 Homebrew 安装 gcc 和 llvm(安装 Homebrew 的说明),或者,如果您已经安装了 gcc/llvm,请跳到下一步
# WARNING: This can take several hours
brew install gcc
brew install llvm
  1. 如果您已经通过 Homebrew 安装了 gcc 和 llvm:
brew cleanup
brew update
brew upgrade
brew reinstall gcc
brew reinstall llvm
  1. 将一些标题链接到 /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
  1. 编辑您的文件(如果您的目录中~/.R/Makevars没有调用文件,请创建它)并仅包含以下行:Makevars~/.R/
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++

LLVM_LOC = /usr/local/opt/llvm
CC=/usr/local/gfortran/bin/gcc -fopenmp
CXX=/usr/local/gfortran/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/usr/local/opt/gettext/lib -L$(LLVM_LOC)/lib -Wl,-rpath,$(LLVM_LOC)/lib
CPPFLAGS=-I/usr/local/opt/gettext/include -I$(LLVM_LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
  1. 在 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")

推荐阅读