“致命错误:找不到‘omp.h’文件"在 Apple M1 上使用 clang [英] "fatal error: 'omp.h' file not found" using clang on Apple M1

查看:136
本文介绍了“致命错误:找不到‘omp.h’文件"在 Apple M1 上使用 clang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试使用 openMP 标志进行编译时,Clang 都无法找到 omp.h.这就是我想要做的

Clang isn't able to find omp.h whenever I try to compile with openMP flag. Here's what I'm trying to do

clang++ -dynamiclib -I/opt/homebrew/Cellar/eigen/3.3.9/include/eigen3/ -Xpreprocessor -fopenmp -o libfoo.dylib foolibrary.cpp -lomp

虽然我在 /opt/homebrew/Cellar/libomp/11.0.1/include/omp.h 中有 omp.h,而 clang 在 usr/bin/clang代码>.我需要配置什么才能找到它吗?

Although I have omp.h in /opt/homebrew/Cellar/libomp/11.0.1/include/omp.h, while clang is in usr/bin/clang. Is there anything I need to configure so that it's able to find it?

推荐答案

当使用 brew 本地针对 M1 时,brew 安装的 clang not/usr/bin.brew 说明 表明它的首选位置是 /opt/homebrew./usr/bin 中的 clang 是指向 Apple 命令行工具之一的链接;你可以通过使用 -v 看到这一点.

When using brew to target the M1 natively, the brew installed clang is not in /usr/bin. The brew instructions show that the preferred location for it is /opt/homebrew. The clang in /usr/bin is a link to the Apple command line tools one; you can see that by using -v.

$ /usr/bin/clang -v
/usr/bin/clang -v
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: arm64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

要安装 brew,您需要按照 brew 在安装时给您的说明进行操作:-

To get the brew installed clang you need to follow the instructions brew gave you when you installed it :-

llvm is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have llvm first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.profile

For compilers to find llvm you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/llvm/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/llvm/include"

这样做可以解决包含问题,但至少对我来说,不是链接库的问题.

Doing that resolves the include issue, but, at least for me, not the library linking one.

/opt/homebrew/opt/llvm/bin/clang -v -fopenmp hello_omp.c
clang version 11.1.0
Target: arm64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin
 "/opt/homebrew/Cellar/llvm/11.1.0/bin/clang-11" -cc1 -triple arm64-apple-macosx11.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello_omp.c -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-rounding-math -munwind-tables -fcompatibility-qualified-id-block-type-checking -target-cpu apple-a7 -target-feature +fp-armv8 -target-feature +neon -target-feature +crypto -target-feature +zcm -target-feature +zcz -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -fallow-half-arguments-and-returns -debugger-tuning=lldb -target-linker-version 609.8 -v -resource-dir /opt/homebrew/Cellar/llvm/11.1.0/lib/clang/11.1.0 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /opt/homebrew/Cellar/llvm/11.1.0/lib/clang/11.1.0/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -fdebug-compilation-dir /Users/jcownie/tmp -ferror-limit 19 -fmessage-length=163 -fopenmp -fopenmp-cuda-parallel-target-regions -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fmax-type-align=16 -o /var/folders/lt/nf3dtk8j16qfsl97d_vgv4dw000lts/T/hello_omp-bd4ce3.o -x c hello_omp.c
clang -cc1 version 11.1.0 based upon LLVM 11.1.0 default target arm64-apple-darwin20.3.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /opt/homebrew/Cellar/llvm/11.1.0/lib/clang/11.1.0/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /opt/homebrew/Cellar/llvm/11.1.0/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 11.0.0 0.0.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o a.out /var/folders/lt/nf3dtk8j16qfsl97d_vgv4dw000lts/T/hello_omp-bd4ce3.o -lomp -lSystem /opt/homebrew/Cellar/llvm/11.1.0/lib/clang/11.1.0/lib/darwin/libclang_rt.osx.a
ld: library not found for -lomp
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

如果你明确地传递推荐的$LDFLAGS,那么事情就可以了...

If you explicitly pass the recommended $LDFLAGS, then things work...

$ echo $LDFLAGS
-L/opt/homebrew/opt/llvm/lib
$ /opt/homebrew/opt/llvm/bin/clang $LDFLAGS -fopenmp hello_omp.c
/opt/homebrew/opt/llvm/bin/clang $LDFLAGS -fopenmp hello_omp.c
$ ./a.out
./a.out
Hello from thread 0
Hello from thread 5
Hello from thread 4
Hello from thread 1
Hello from thread 3
Hello from thread 2
Hello from thread 7
Hello from thread 6
$ 

我的(hacky,但它有效)解决方案是创建我自己的 shell 脚本以使用适当的库路径调用编译器;你可以在 How to build LLVM (clang,clang++) 苹果 M1?

My (hacky, but it works) solution to that has been to create my own shell scripts to invoke the compilers with the appropriate library path; you can see this hack in How to build LLVM (clang,clang++) for Apple M1?

还有一点需要注意的是,目前针对 M1 的 LLVM 似乎破坏了对 OpenMP 任务的支持.(参见将 firstprivate 参数传递给 Apple M1 上的任务时崩溃";).

One more point to beware of is that the support for OpenMP tasks seems to be broken in LLVM when targeting the M1 at present. (See "Crash in passing firstprivate args to tasks on Apple M1").

这篇关于“致命错误:找不到‘omp.h’文件"在 Apple M1 上使用 clang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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