I am wondering which is the recommended way to enable OpenMP support, regardless of the OS.
I mostly work on Windows OS, and there it was only “sufficient” to set the target compile options to include the required flag.
I have very recently started to move to UNIX OSes, and there I realised that was not working anymore (failing at link time). So, I went for the find_package route. Solved. But then, this is apparently not working on Windows again, in which CMake is unable to find OpenMP.
This approach is also mentioned here. Here is a similar question, but for MacOS.
So. What is the best (and unique?) way to enable OpenMP support? I would say using the intrinsic module provided FindOpenMP, but I don’t understand why it is not working in this case.
The code:
find_package( OpenMP )
if ( NOT OpenMP_Fortran_FOUND )
message( FATAL_ERROR "OpenMP not found" )
endif()
target_link_libraries( ${project} PUBLIC OpenMP::OpenMP_Fortran )
is the canonical way. Indeed, if you add REQUIRED then there is no need to test if found, CMake will stop at that point if it can’t find OpenMP support.