gcc
and g++
that come with Xcode are nothing but Apple's Clang under disguise:
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
--> Apple clang version 11.0.3 (clang-1103.0.32.59) <--
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Modern versions of Clang do support OpenMP, and the one from Apple does too. But:
- it doesn't directly understand the
-fopenmp
option, although it is listed in the output of clang --help
- it doesn't provide the OpenMP runtime
There is a workaround for 1. Instead of -fopenmp
, pass -Xclang -fopenmp
. But that is not enough - it will result in either the compiler failing to find omp.h
or produce link errors with missing symbols. This is because -Xclang -fopenmp
enables processing and transformation of OpenMP pragmas, but one needs the OpenMP runtime too.
For 2, you may install libomp
from Homebrew - the open-source version of Intel OpenMP runtime, which is now LLVM OpenMP runtime. I guess, when you say you "installed OpenMP using brew" you actually mean you installed libomp
using brew
.
Having libomp
installed allows you to compile OpenMP code, because /usr/local/include
(where Homebrew symlinks omp.h
) is on the compiler's include path, but you have to link the library explicitly:
$ g++ -Xclang -fopenmp foo.cc -lomp
This is specific to Apple's Clang compiler. Normally, when you use true GCC or Clang to both compile and link the code, i.e., without a separate step using the linker to link a bunch of object files, -fopenmp
enables both processing of OpenMP pragmas and also tells the linker to link the OpenMP runtime and you don't need to specify -lomp
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…