在 Apple M1(arm)上使用 cmake 为 macOS-x86_64 构建的正确方法是什么?

What is the proper way to build for macOS-x86_64 using cmake on Apple M1 (arm)?

我正在使用无法为 Apple M1 编译的库,因此我决定编译它并使用 (Rosetta 2) 为 x86_64 使用它,我在 之后成功地做到了为 x86_64.

安装 brew 和 clang

然而,当我编译我的项目并尝试 link 它针对这个库时,我得到这个错误:

ld: warning: ignoring file ..../libapronxx.a, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
...
ld: symbol(s) not found for architecture arm64

我已经尝试设置编译器和 linker 标志(“-arch x86_64”)但仍然遇到同样的问题。

我的问题是:在 Apple M1(arm)上使用 cmake 构建 macOS-x86_64 的正确方法是什么?

其他信息:我正在通过 CLion 使用 cmake。

更新: 我使用以下命令成功编译了我的项目:

# install a x86 cmake
arch -x86_64 /usr/local/bin/brew install cmake
...
# in the build directory
arch -x86_64 /usr/local/bin/cmake ..
make
arch -x86_64 ./my_exe

我还使用 -arch 标志为 clang 指定了架构

string(APPEND CMAKE_CXX_FLAGS_RELEASE " -arch x86_64 -O3")
string(APPEND CMAKE_CC_FLAGS_RELEASE " -arch x86_64 -O3")
# did the same for debug too

虽然这项工作很好,但我仍然不认为这是使用 cmake 为 macOS 构建的正确方法-x86_64,事实上我无法利用我的 IDE所有这些手动方法。

查看CMake源码后,发现设置CMAKE_OSX_ARCHITECTURESx86_64即可:

set(CMAKE_OSX_ARCHITECTURES "x86_64")

这是迄今为止解决这个问题的最干净的方法,也可以直接使用 CLion。