在 macOS arm64 架构上使用 x86 库和 OpenMP [英] Using x86 libraries and OpenMP on macOS arm64 architecture

查看:100
本文介绍了在 macOS arm64 架构上使用 x86 库和 OpenMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a MacBook M1 and have installed a library on my machine that was compiled for an x86 / Intel architecture. I have some source code that uses OpenMP. I would like to compile my code and link my executable to the x86 library using a clang compiler.

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

I am able compile source code with no x86 dependencies by following the instructions here, using an implementation of clang that is distributed with brew.

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

However when I try to compile with the -arch x86_64 argument and link to the x86 library I find that clang tries to link my executable to an OpenMP library that is built for the arm64 architecture.

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

Is it possible to install a version of clang on a MacBook M1 where the OpenMP libraries are built for x86 architectures?

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

Here is an example of an error that I get using my current setup, even when not linking to an x86 library.

源代码:

#include <omp.h>
int main()
{
    return 0;
}

调用编译器:

/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

错误信息:

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)

推荐答案

使用 brew 的 x86 安装解决了我的问题.这是用于安装 brew 和 clang 的 x86 变体,然后编译我的 C/C++ 代码的最小命令集:

Using an x86 installation of brew solves the problem for me. Here is a minimal set of commands for installing x86 variants of brew and clang, and then compiling my C/C++ code:

# 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

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

The brew application is now installed in two separate locations on my machine:

# arm64 (default) location
/opt/homebrew/bin/brew
# x86_64 location
/usr/local/bin/brew

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

and clang is installed in three separate locations:

# 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

这篇关于在 macOS arm64 架构上使用 x86 库和 OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆