How to set linker flags for OpenMP in CMake's try_compile function

后端 未结 4 1118
面向向阳花
面向向阳花 2020-11-29 01:25

I would like to verify that the current compiler can build with openmp support. The application has do deploy across a wide variety of unix systems, some of which might hav

4条回答
  •  借酒劲吻你
    2020-11-29 01:37

    As of CMake 3.9 there are imported OpenMP targets per language. I consider this to be a much more elegant solution. Here's an example in C++:

    cmake_minimum_required(VERSION 3.9)
    project(solver LANGUAGES CXX)
    
    find_package(OpenMP REQUIRED)
    add_executable(solver solver.cc)
    target_link_libraries(solver PRIVATE OpenMP::OpenMP_CXX)
    

    This is more convenient since it is less typing, and this way you don't have to adjust with compile flags, libraries, etc which are error-prone. This is the direction that modern CMake is going.


    If you are working with something older than CMake 3.9 I still don't recommend the currently accepted answer. I believe setting the flags per-target is better:

    add_executable(solver solver.cc)
    target_link_libraries(solver PRIVATE "${OpenMP_CXX_FLAGS}")
    target_compile_options(solver PRIVATE "${OpenMP_CXX_FLAGS}")
    

    This may not work with some compilers; this is partly why CMake revamped its OpenMP support in CMake 3.9.

提交回复
热议问题