1

I recently installed OpenMP on my macOS high Sierra using brew.

I can easily run code that has OpenMP directives using g++-9 (similar to what was suggested in the answer here: Using OpenMP with C++11 on Mac OS ) . However, I need to add OpenMP functionality to a project that uses OpenCV, and I can only compile that with regular g++ ( g++ —version shows it’s 4.2.1) . I do not intend to use any OpenCV built-in algorithms that may use OpenMP, simply want to use them separately in the same program to optimize some image processing algorithms on my own. How can I make OpenMP be able to run with the other version of g++? Doing something like:

g++ hello_world.cpp -fopenmp

Gives:

clang: error: unsupported option '-fopenmp'
Kaspar
  • 11
  • 3
  • OpenMP is a feature of the compiler - it either supports OpenMP or it doesn't. There is no way to install OpenMP. You can install a compiler that has OpenMP support, but you cannot install OpenMP support for a compiler that doesn't already have such. `g++` 4.2.1 in older versions of Xcode is the Apple's GCC frontend for LLVM and it is not built with OpenMP support. You have to use the compiler you installed from Homebrew. – Hristo Iliev May 18 '20 at 08:11
  • 1
    What @HristoIliev said! To be clear: 1) OpenMP is implemented by a compiler. 2) To confuse people, Apple present a compiler with the name "gcc" which is *not* gcc, but rather clang. 3) You need to be consistent in which compiler you use (it sounds as though you want to use gcc ), in which case you need to ensure that that is earlier on your $PATH than Apple's fake gcc... (You could very likely equally use clang throughout if you installed a modern clang, since LLVM now has OpenMP support too, but if you want GCC go with GCC). – Jim Cownie May 18 '20 at 12:30

1 Answers1

3

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:

  1. it doesn't directly understand the -fopenmp option, although it is listed in the output of clang --help
  2. 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.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186