Mac M1平台编译CMake源码失败,寻求解决方法
Mac M1编译CMake时的链接错误解决步骤
环境信息
- 系统架构:
$ arch arm64
- clang版本:
$ clang --version Apple clang version 14.0.0 (clang-1400.0.29.201) Target: arm64-apple-darwin22.1.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin
- clang++版本:
$ clang++ --version Apple clang version 14.0.0 (clang-1400.0.29.201) Target: arm64-apple-darwin22.1.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin
- CMake源码来源:Kitware/CMake的GitHub仓库master分支
- 当前PATH配置:
/Library/Developer/CommandLineTools/usr/bin:/usr/bin:
问题重现
执行以下命令尝试编译CMake:
export CC=clang export CXX=clang++ ./configure --prefix=/usr/local/cmake/3.25.1
触发bootstrap错误:
--------------------------------------------- CMake 3.25.1, Copyright 2000-2022 Kitware, Inc. and Contributors --------------------------------------------- Error when bootstrapping CMake: Cannot find appropriate C compiler on this system. Please specify one using environment variable CC. See cmake_bootstrap.log for compilers attempted. --------------------------------------------- Log of errors: cmake-3.25.1/Bootstrap.cmk/cmake_bootstrap.log ---------------------------------------------
查看错误日志cmake-3.25.1/Bootstrap.cmk/cmake_bootstrap.log,核心错误为:
ld: unknown option: --rpath=/path/to/opt/anacounda3/lib:-L/path/to/opt/anaconda3/lib: clang: error: linker command failed with exit code 1 (use -v to see invocation) Test failed to compile
手动测试链接命令时也出现错误:
clang -Wl,-rpath,/path/to/opt/anacounda3/lib:-L/path/to/opt/anaconda3/lib: -framework CoreFoundation
返回:
Undefined symbols for architecture arm64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
解决步骤
1. 清理Anaconda残留的环境变量干扰
错误根源是环境中残留的Anaconda相关链接/编译参数(如LDFLAGS),导致CMake的bootstrap过程错误拼接了无效的链接器选项。执行以下命令临时清理这些变量:
unset LDFLAGS unset CPPFLAGS unset CFLAGS unset CXXFLAGS
2. 重新执行配置命令
清理环境变量后,重新指定编译器并运行configure:
export CC=clang export CXX=clang++ ./configure --prefix=/usr/local/cmake/3.25.1
3. 完成编译与安装
如果configure成功,继续执行编译(利用多核加速)和安装:
make -j$(sysctl -n hw.ncpu) sudo make install
4. 可选:永久消除环境变量干扰
若不想每次编译都手动清理,可检查终端配置文件(如~/.zshrc、~/.bash_profile),移除其中Anaconda相关的LDFLAGS、CPPFLAGS等配置项,保存后重启终端即可。
内容的提问来源于stack exchange,提问作者newkid
相关产品推荐
相关产品推荐

