在 macOS arm64 架构上使用 x86 库和 OpenMP

Phi*_*ank 8 c++ macos openmp clang apple-m1

我有一台 MacBook M1,并在我的机器上安装了一个针对 x86 / Intel 架构编译的库。我有一些使用 OpenMP 的源代码。我想使用 clang 编译器编译我的代码并将我的可执行文件链接到 x86 库。

我可以按照此处的说明,使用与brew一起分发的clang实现来编译没有x86依赖项的源代码。

然而,当我尝试使用-arch x86_64参数进行编译并链接到 x86 库时,我发现 clang 尝试将我的可执行文件链接到为 arm64 架构构建的 OpenMP 库。

是否可以在 MacBook M1 上安装为 x86 架构构建 OpenMP 库的 clang 版本?

以下是我使用当前设置时遇到的错误示例,即使未链接到 x86 库也是如此。

源代码:

#include <omp.h>
int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

调用编译器:

/opt/homebrew/opt/llvm/bin/clang++ -arch x86_64 omp_ex.cpp \ 
    -L/opt/homebrew/opt/llvm/lib -Wl,-rpath,/opt/homebrew/opt/llvm/lib \
    -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
Run Code Online (Sandbox Code Playgroud)

错误信息:

ld: in '/opt/homebrew/opt/llvm/lib/libunwind.dylib', building for macOS-x86_64 but attempting to link with file built for macOS-arm64
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

Phi*_*ank 19

使用 x86 安装的brew 解决了我的问题。以下是安装 x86 版本的 brew 和 clang,然后编译我的 C/C++ 代码的最小命令集:

# launch x86_64 shell
arch -x86_64 zsh  
# install x86_64 variant of brew 
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# install x86_64 variant of clang
arch -x86_64 /usr/local/bin/brew install llvm 
# compile using x86_64 variant of clang
/usr/local/opt/llvm/bin/clang++ -arch x86_64 omp_ex.cpp
Run Code Online (Sandbox Code Playgroud)

现在,brew 应用程序安装在我的计算机上的两个不同位置:

# arm64 (default) location
/opt/homebrew/bin/brew
# x86_64 location
/usr/local/bin/brew
Run Code Online (Sandbox Code Playgroud)

clang 安装在三个不同的位置:

# Apple arm64 (default) location
/usr/bin/clang
# brew arm64 location
/opt/homebrew/opt/llvm/bin/clang
# brew x86_64 location
/usr/local/opt/llvm/bin/clang
Run Code Online (Sandbox Code Playgroud)