Summary
How to compile C ++ code from OpenMP on Mac OSX in portable mode?
There are many sources that offer solutions for compiling C ++ with OpenMP in OSX, for example:
Most of them suggest installing a later LLVM / Clang (or GCC) instead of the standard Clang. On OSX 10.12.6 (Sierra), using LLVM (via brew install llvm ) works for me.
However, the resulting binary does not seem portable. If possible, I want to provide binary code so that my users cannot compile themselves.
Here is what I tried
Running otool -L my_binary gives
/usr/local/opt/llvm/lib/libomp.dylib (compatibility version 5.0.0, current version 5.0.0) /usr/local/opt/llvm/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0) /usr/lib/libc++abi.dylib (compatibility version 1.0.0, current version 307.2.0)
The first two lines do not look as if I can just pass this binary code to some user and expect it to work. First, the user will need to install LLVM.
So, I found that install_name_tool is able to change this. See https://blogs.oracle.com/dipol/dynamic-libraries,-rpath,-and-mac-os
So I ran
cp /usr/local/opt/llvm/lib/libomp.dylib . cp /usr/local/opt/llvm/lib/libc++.1.dylib . install_name_tool -change /usr/local/opt/llvm/lib/libomp.dylib @executable_path/libomp.dylib my_binary install_name_tool -change /usr/local/opt/llvm/lib/libc++.1.dylib @executable_path/libc++.1.dylib my_binary install_name_tool -id "@loader_path/libomp.dylib" libomp.dylib install_name_tool -id "@loader_path/libc++.1.dylib" libc++.1.dylib
Unfortunately, I do not have another Mac to test this. So I don’t even know if this works.
Questions
Is this the right way to do this? It is somehow not good to change these two libraries in this way ... What is the “usual” solution for this problem?
An additional minor problem: CMake does not find OpenMP (using find_package ), so I need to hard -fopenmp=libomp necessary flag ( -fopenmp=libomp ). This flag is actually used by CMake, but is not recognized as working. Any idea why, or how to fix it?