CMake cannot find OpenMP

前端 未结 4 2026
南笙
南笙 2021-02-19 02:07

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

find_package(OpenMP REQUIRED)

and CMake errors out with

相关标签:
4条回答
  • 2021-02-19 02:37

    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

    0 讨论(0)
  • 2021-02-19 02:49

    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.

    0 讨论(0)
  • 2021-02-19 03:02

    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()
    
    0 讨论(0)
  • 2021-02-19 03:02

    You should install libomp with brew install libomp

    i use macOS and it worked smoothly for me.

    0 讨论(0)
提交回复
热议问题