CMake cannot find OpenMP

OpenMp is not a package, if it's supported, it comes as a part of the your compiler. Try setting CMAKE_C_FLAGS or CMAKE_CXX_FLAGS accordingly. e.g:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp") activates OpenMP for compiling C sources when gcc is used. For other compilers, you should first detect the compiler and then add appropriate flags


According to the Modern CMake online book, this is how you configure OpenMP support with CMake:

find_package(OpenMP)
if(OpenMP_CXX_FOUND)
    target_link_libraries(MyTarget PUBLIC OpenMP::OpenMP_CXX)
endif()

What you definitely should not do is to add flags like -fopenmp manually (like the accepted answer recommends) because that may not be portable.


CMake has a FindOpenMP module even in 2.x versions. See http://www.cmake.org/cmake/help/v3.0/module/FindOpenMP.html

So I'll do this:

OPTION (USE_OpenMP "Use OpenMP" ON)
IF(USE_OpenMP)
  FIND_PACKAGE(OpenMP)
  IF(OPENMP_FOUND)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
  ENDIF()
ENDIF()

Tags:

Cmake

Openmp