Compile C ++ with OpenMP on Mac OSX with dynamic binding

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?

+4
source share
1 answer

Yes, you need to change the location of dylib in the executable if you want to associate them with the application. Please note that you do not “modify these two libraries”, but only specify the search path inside your executable file.

Regarding the second point (CMake does not find OpenMP): this should be resolved with newer versions of cmake (> = 3.12). On my system (OSX 10.13), the following entries in CMakeLists.txt do the trick:

 find_package(OpenMP) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") if (APPLE) target_link_libraries(my_target OpenMP::OpenMP_CXX) else () target_link_libraries(my_target) endif() 
0
source

Source: https://habr.com/ru/post/1270862/


All Articles