Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

opencv - Running OpenMP with different version of g++ in Mac

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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...