Apple Silicon(ARM)平台交叉编译x64程序遇链接错误求助
问题背景
在Apple Silicon(ARM64)系统上编译生成Intel x86_64架构的目标文件是可行的(例如Xcode构建通用包就采用该机制),但使用make编译Stockfish时无法完成交叉编译。
已尝试操作
- 执行命令:
make build ARCH=x86-64-modern COMP=clang,将ARCH替换为apple-silicon时可正常编译 - 尝试使用
gcc编译器,针对apple-silicon架构编译同样可行
错误信息
执行make build ARCH=x86-64-modern COMP=clang后,出现大量Bitcode相关警告,随后触发链接错误:
ld: warning: ignoring file
.o, lto file was built for unknown bitcode architecture which is not the architecture being linked (x86_64): .o
关键链接错误内容:
Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
解决方案
问题核心是交叉编译时未正确指定目标架构,导致部分目标文件仍为ARM64架构,链接器无法识别这些文件,最终出现符号缺失。可通过以下步骤修复:
强制指定目标架构编译参数
在make命令中手动添加clang的交叉编译目标参数,确保所有源文件都编译为x86_64架构:make build ARCH=x86-64-modern COMP=clang CXXFLAGS="-target x86_64-apple-macos10.15" LDFLAGS="-target x86_64-apple-macos10.15"其中
x86_64-apple-macos10.15为目标架构的系统标识,版本号可根据你的macOS版本调整。清理旧目标文件
之前编译生成的ARM64架构.o文件会干扰交叉编译的链接过程,先执行清理:make clean再重新执行上述编译命令。
关闭Bitcode优化
错误中的Bitcode警告源于交叉编译时Bitcode的架构识别异常,可通过添加编译参数关闭Bitcode:make build ARCH=x86-64-modern COMP=clang CXXFLAGS="-target x86_64-apple-macos10.15 -fno-bitcode" LDFLAGS="-target x86_64-apple-macos10.15 -fno-bitcode"检查并修改Makefile配置
打开Stockfish的Makefile,确认x86-64-modern对应的编译配置是否包含正确的目标架构参数。若Makefile未为交叉编译设置-target,可手动在该架构的配置段中添加-target x86_64-apple-macos参数。
内容的提问来源于stack exchange,提问作者CryptoAlgorithm

