2

I know the way to use cmake to link openmp in a cross-platform way

find_package(OpenMP REQUIRED)
link_libraries(OpenMP::OpenMP_CXX)

But I don't know how to force cmake to static link openmp, in fact, all of cmake official variable about openmp library is all dynamic.
Anyway, The non-cross-platform way to do so is:

clang++ -std=c++2a test.cpp -Iinclude -march=native -O3 -c          
clang++ test.o -o test.x /usr/local/lib/libomp.a -pthread  

or if you use gcc

g++-10 -std=c++2a test.cpp -Iinclude -march=native -O3 -c 
g++-10 test.o -o test.x /usr/local/opt/gcc/lib/gcc/10/libgomp.a -pthread 

By the way, is it a cmake defect or is there any other way to accomplish it

Wongboo
  • 124
  • 1
  • 6
  • 1
    Static linking the OpenMP library is potentially a bad idea. If you do that, and also use a library that itself does it you will end up with two instances of the OpenMP runtime, and that can lead to over-subscription (creating more threads than you have logicalCPUs) and, even, incorrect execution (e.g. an onp single will only guard the code from code that is using the same library instance). So, if you do this, be very careful... – Jim Cownie Nov 16 '20 at 08:53
  • @JimCownie I understand your idea, your comment may be the answer of question, some libs aren't suitable to link statically, such as OpenMP – Wongboo Sep 09 '22 at 02:22

1 Answers1

0

Not an answer but too much to fit into a comment.

I don't know anything about OpenMP other than cmake does support it: https://cmake.org/cmake/help/latest/module/FindOpenMP.html?highlight=openmp.

I don't see any of the documentation referring to static/shared. Maybe you are correct and it only support shared libs.

Double check by asking the official make discourse: https://discourse.cmake.org/

You could also try reading the official FindOpenMP.cmake module since this is all open source.

EDIT:

If you are correct cmake is lacking this functionality consider contributing and adding it :)

  • Yes, I try to message out all variable in [link](https://cmake.org/cmake/help/latest/module/FindOpenMP.html?highlight=openmp), only to find out all variable related to lib refer to dynamic lib, if that is the case, maybe I should file a bug to CMake official later – Wongboo Nov 15 '20 at 02:16
  • @Wongboo Did you ever figure this one out or posted the bug? I too want to static link OpenMP. – Teharez Jun 17 '21 at 06:41
  • @Teharez, I think Jim's comment is right, I suggest you not static link openmp, if you insist, I write a way for macOS, try use it. – Wongboo Oct 21 '21 at 13:35