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

Wae*_*lay 26 c++ linker cmake cross-compiling apple-silicon

我正在使用一个无法为 Apple M1 编译的库,因此我决定编译它并使用 x86_64 的 (Rosetta 2) 使用它,我成功地按照此x86_64 安装了brew 和 clang。

但是,当我编译我的项目并尝试将其链接到该库时,我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

我尝试设置编译器和链接器标志(“-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
Run Code Online (Sandbox Code Playgroud)

我还使用 -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
Run Code Online (Sandbox Code Playgroud)

虽然这工作正常,但我仍然不相信这是使用 cmake 为 macOS-x86_64 构建的正确方法,事实上,我无法通过所有这些手动方法来利用我的 IDE 的优势。

Wae*_*lay 38

检查CMake源代码后,我发现设置CMAKE_OSX_ARCHITECTURESx86_64

set(CMAKE_OSX_ARCHITECTURES "x86_64")
Run Code Online (Sandbox Code Playgroud)

这是迄今为止解决此问题的最简洁的方法,并且也可以直接与 CLion 合作。

  • 我没有让它正常工作,需要执行此`set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE INTERNAL "" FORCE)` (5认同)
  • @DuncanGroenewald,“set(...)”是一个 CMake 命令,应放置在“CMakeLists.txt”文件的开头。 (2认同)