How to Include Omp.H in Os X

How to include omp.h in OS X?

GCC 4.9.1 normally does not ship with OS X (actually no GCC ships with Xcode any more). Yours must have been installed by another means, e.g. Homebrew or self compilation as described here. What you are probably missing is properly set PATH variable or the additionally installed compiler has version-suffixed binaries, i.e. gcc-4.9 or g++-4.9 instead of simply gcc / g++.

As @rubenvb has already mentioned, Apple symlinks the Clang executables with GCC-like names. I personally find that a bad practice since recent Clang versions shipped with Xcode react on unrecognised command-line options (e.g. GCC frontend specific ones) with hard errors.

fatal error: 'omp.h' file not found using clang on Apple M1

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

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)

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
$

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?

Note that at one more point the support for OpenMP tasks was broken in LLVM when targeting the M1. (See "Crash in passing firstprivate args to tasks on Apple M1"). However, that was fixed in March 2021, so be sure to use a more recent LLVM release.

how to include omp.h without specifying exact path on OS X

You should be able to do it with a command line option for the C or C++ compiler:

-I /usr/local/Cellar/libiomp/20150401/include/libiomp

(space between -I and path optional) on the compiler command line. You might well have a variable that identifies /usr/local/Cellar/libiomp/20150401 as the location where the OMP code is installed, because the chances are you also need an option -L /usr/local/Cellar/libiomp/20150401/lib (space optional again) to pick up the libraries when you link, too.

Fixing that in your CMakeLists.txt file is a somewhat separate discussion, but you're likely to be able to find some exemplars that will help there.

OpenMP support on OSX 10.11, gcc errors with file omp.h not found

I changed the compiler and now I am able to run it. [It was issue of clang, which I couldn't solve].
I am a student and Intel is giving Intel Parallel Studio 1 year licence for free to students.
So I downloaded, and installed it.
In xCode, under build settings, I set my compiler to 'Intel C/C++ compiler' and in parallalization, I turned it to 'yes'. That was it. Then it compiled successfully. But, note that you won't be using header file 'omp.h' anymore.
By the way, I am still looking for answers, just to know what I was doing wrong.



Related Topics



Leave a reply



Submit