25

I am trying to compile with OpenMP. My CMakeLists.txt contains the line

find_package(OpenMP REQUIRED)

and CMake errors out with

CMake Error at /opt/ros/groovy/share/catkin/cmake/catkinConfig.cmake:72 (find_package):
  Could not find a configuration file for package openmp.

  Set openmp_DIR to the directory containing a CMake configuration file for
  openmp.  The file will have one of the following names:

    openmpConfig.cmake
    openmp-config.cmake

Checking my filesystem, I see that I have /usr/share/cmake-2.8/Modules/FindOpenMP.cmake but no openmpConfig.cmake or openmp-config.cmake. What do I need to do to fix this?

ehuang
  • 811
  • 1
  • 8
  • 17

5 Answers5

36

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()
iNFINITEi
  • 1,504
  • 16
  • 23
12

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.

András Aszódi
  • 8,948
  • 5
  • 48
  • 51
  • 3
    This is the ONLY correct answer on this page. – Alex Reinking May 11 '21 at 17:26
  • 1
    The pastes cmake lines is cool, brief, portable. However, it failed on my qnx-aarch64 compiler, it just don't know where the openmp shared or static library locates, but I know where they are. What's worse, I can't tell CMake to find them! What a joke. – ChrisZZ Jan 13 '22 at 15:14
  • That only sets the link libraries. My cmake successfully finds OpenMP, but does not add a flag to the compile line. – Victor Eijkhout Aug 01 '22 at 13:46
7

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

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
mmirzadeh
  • 6,893
  • 8
  • 36
  • 47
  • 2
    http://stackoverflow.com/questions/3134660/how-to-apply-different-compiler-options-for-different-compilers-in-cmake had an interesting solution in which CMake checks if the flag is supported using a macro. Sort of the logical expansion of your solution. – JonnyRo Jul 15 '13 at 03:55
  • 4
    There is FindOpenMP cmake module. See http://www.cmake.org/cmake/help/v3.0/module/FindOpenMP.html – iNFINITEi Mar 01 '15 at 10:39
  • @GradGuy I am using CMake 3.7 and clang 4.0 but my `find_package(OpenMP REQUIRED)` fails to find OpenMP. – David Doria Nov 22 '16 at 01:55
  • @DavidDoria Clang used to have problems with OpenMP, specially on OSX, but it looks like the new version should work fine. Can you build a simple OpenMP test with your version of clang? – mmirzadeh Dec 02 '16 at 18:59
  • @GradGuy You mean with Clang I don't even have to `find_package(OpenMP)`? – David Doria Dec 02 '16 at 23:18
0

iNFINITEi's answer doesn't work for me. I use Ubuntu, trying to compile some code with OpenCV static library. After linking, I got this:

'"/usr/bin/ld: /usr/local/lib/libopencv_core.a(parallel.cpp.o): undefined reference to symbol 'omp_set_dynamic@@OMP_1.0'"'

So I tried iNFINITEi's approach, then I have:

'CMake Error at /usr/local/share/cmake-3.13/Modules/FindPackageHandleStandardArgs.cmake:211 (message): No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS() Call Stack (most recent call first): /usr/local/share/cmake-3.13/Modules/FindOpenMP.cmake:513 (find_package_handle_standard_args) CMakeLists.txt:8 (FIND_PACKAGE)'

At last, I add "-fopenmp=libomp" to CMAKE_CXX_FLAGS, solved my problem.

realr
  • 3,652
  • 6
  • 23
  • 34
Jason Zhao
  • 11
  • 3
-5

You should install libomp with brew install libomp

i use macOS and it worked smoothly for me.