ehuang
ehuang

Reputation: 851

CMake cannot find OpenMP

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?

Upvotes: 27

Views: 37334

Answers (6)

김영호
김영호

Reputation: 1

I suffered from those case and my answers was below. Those two lines were in bashrc. So I erased those two lines and problems got solved.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/openmpi/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu

I searched CMakeCache.txt file. When compile got no problems, there were these lines.

//CXX compiler flags for OpenMP parallelization
OpenMP_CXX_FLAGS:STRING=-fopenmp

//CXX compiler libraries for OpenMP parallelization
OpenMP_CXX_LIB_NAMES:STRING=gomp;pthread

//C compiler flags for OpenMP parallelization
OpenMP_C_FLAGS:STRING=-fopenmp

//C compiler libraries for OpenMP parallelization
OpenMP_C_LIB_NAMES:STRING=gomp;pthread

//Path to the gomp library for OpenMP
OpenMP_gomp_LIBRARY:FILEPATH=/home/kimyoungho/anaconda3/envs/kimyoungho/x86_64-conda-linux-gnu/lib/libgomp.so

//Path to the pthread library for OpenMP
OpenMP_pthread_LIBRARY:FILEPATH=/home/kimyoungho/anaconda3/envs/kimyoungho/x86_64-conda-linux-gnu/sysroot/usr/lib/libpthread.so

When compile got problems, there were these lines.

//CXX compiler flags for OpenMP parallelization
OpenMP_CXX_FLAGS:STRING=-fopenmp

//CXX compiler libraries for OpenMP parallelization
OpenMP_CXX_LIB_NAMES:STRING=gomp;pthread

//C compiler flags for OpenMP parallelization
OpenMP_C_FLAGS:STRING=-fopenmp

//C compiler libraries for OpenMP parallelization
OpenMP_C_LIB_NAMES:STRING=gomp;pthread

//Path to the gomp library for OpenMP
OpenMP_gomp_LIBRARY:FILEPATH=/usr/lib/gcc/x86_64-linux-gnu/11/libgomp.so

//Path to the pthread library for OpenMP
OpenMP_pthread_LIBRARY:FILEPATH=/usr/lib/x86_64-linux-gnu/libpthread.a

I think that LD_LIBRARY_PATH matters anyway.

Upvotes: 0

András Aszódi
András Aszódi

Reputation: 9660

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.

Upvotes: 17

Prakhar Lohiya
Prakhar Lohiya

Reputation: 9

You should install libomp with brew install libomp

i use macOS and it worked smoothly for me.

Upvotes: -5

Jason Zhao
Jason Zhao

Reputation: 11

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.

Upvotes: 0

mmirzadeh
mmirzadeh

Reputation: 7079

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

Upvotes: 6

iNFINITEi
iNFINITEi

Reputation: 1534

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

Upvotes: 36

Related Questions