CMake: How can I force cmake to static link openmp?

Viewed 727

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

1 Answers

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 :)

Related