使用 ARM M1 的 Mac 上的 cmake 是具有 x86_64 架构而不是 arm64 的 运行 链接器

cmake on Mac with ARM M1 is running linker with x86_64 architecture instead of arm64

我正在尝试使用 M1 arm64 处理器从 Mac 上的源代码编译 glfw,而 运行 linker,cmake 奇怪地试图 link x86_64 架构的项目,而二进制文件是为 arm64 构建的。

我克隆项目,创建名为 cmake-build-debug 的构建文件夹,在其中使用 Makefile 等生成构建系统,如下所示:

git clone https://github.com/glfw/glfw.git
cd glfw
mkdir cmake-build-debug
cd cmake-build-debug
cmake -S .. -B . -DCMAKE_BUILD_TYPE=Debug -DCMAKE_HOST_SYSTEM_PROCESSOR=arm64 -DCMAKE_SYSTEM_PROCESSOR=arm64

这很好用。但是现在我使用 makecmake --config Debug --build . 构建它,.o 二进制文件生成得非常好,但是 linker 脚本被 cmake 使用 x86_64 错误地调用出于某种原因的目标架构:

-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) 
-- Including Cocoa support
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/burkov/Documents/Projects/open-source/glfw/cmake-build-debug
[ 47%] Built target glfw
Scanning dependencies of target wave
[ 50%] Linking C executable wave.app/Contents/MacOS/wave
ld: warning: ignoring file CMakeFiles/wave.dir/wave.c.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file ../src/libglfw3.a, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
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)
make[2]: *** [examples/wave.app/Contents/MacOS/wave] Error 1
make[1]: *** [examples/CMakeFiles/wave.dir/all] Error 2
make: *** [all] Error 2

我查看 glfw/cmake-build-debug/examples/CMakeFiles/wave.dir/build.make 中失败的 Makefile 并看到 cmake 崩溃的行:

cd /Users/me/Documents/Projects/open-source/glfw/cmake-build-debug/examples && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/wave.dir/link.txt --verbose=$(VERBOSE)

我手动打开文件 glfw/cmake-build-debug/examples/CMakeFiles/wave.dir/link.txt 文件,看到下面的 link 脚本代码:

/Library/Developer/CommandLineTools/usr/bin/cc -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/wave.dir/wave.c.o  -o wave.app/Contents/MacOS/wave  ../src/libglfw3.a -framework Cocoa -framework IOKit -framework CoreFoundation 

如果我从 shell 手动执行此行,它会按预期成功构建我的 arm64 架构二进制文件。

但是,当此 link.txt 脚本通过 cmake -E cmake_link_script CMakeFiles/wave.dir/link.txt --verbose=$(VERBOSE) 自动调用 cmake 时,它显然失败了,试图为错误的 x86_64 体系结构构建二进制文件。

为什么会发生这种情况以及如何解决这个问题?

任何 运行 遇到同样问题的人,looks like the first version of cmake with an adequate support for Apple Silicon is 3.19

我使用的是 3.17.5,因为我的 out-of-date 版本的 CLion 不支持高于该版本的 cmake。

更新到 cmake 3.22.4 后问题消失了。