CMake Support for OpenMP on macOS High Sierra

前端 未结 2 1446
逝去的感伤
逝去的感伤 2020-12-19 20:34

I\'m attempting to add OpenMP to a project that is building with CMake. I\'m having no problem building it on Linux with the standard CMake/OpenMP addition:

         


        
2条回答
  •  旧巷少年郎
    2020-12-19 20:54

    I had the same problem and it took one complete day to find the solution. I am using Mac-Sierra 10.13.4. I want to use Opencv3 (I think the same problem also appears for opencv2) and openMP. I was actually using Clion as the IDE (CLion uses cmake to configure the project unlike other IDE), so I have to write the CMakeLists.txt file.

    There was a conflict of using gcc as the compiler for openCV and openMP. If you use gcc as the compiler then it gives error for opencv as :

    imwrite() on OS X error: Undefined symbols You need to specifically use llvm compiler on OS X to resolve this issue. Following, I am giving the correct code for using OpenCV and OpenMP on Mac-Sierra:

    cmake_minimum_required(VERSION 3.10)
    project(MyOpenCVTest)
    
    set(CMAKE_CXX_STANDARD 11)
    
    add_executable(MyOpenCVTest main.cpp)
    
    # set("OpenCV_DIR" "/modules/opencv/3.4.1/share/OpenCV/")
    set(CMAKE_PREFIX_PATH "/usr/local/Cellar/opencv@3/")
    
    set(OpenCV_INCLUDE_DIRS "/usr/local/Cellar/opencv@3/include/")
    set(OpenCV_LIBS "/usr/local/Cellar/opencv@3/lib/")
    
    find_package(OpenCV REQUIRED)
    
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    libraries: ${OpenCV_LIBS}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")
    
    if(UNIX)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
    endif()
    
    
    set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang")
    set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang++")
    set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/6.0.0/lib")
    set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/6.0.0/include")
    
    OPTION (USE_OpenMP "Use OpenMP to enamble " ON)
    
    # Find OpenMP
    if(APPLE AND USE_OpenMP)
        if(CMAKE_C_COMPILER_ID MATCHES "Clang")
            set(OpenMP_C "${CMAKE_C_COMPILER}")
            set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
            set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
            set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
            set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
            set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
        endif()
        if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
            set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
            set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
            set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
            set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
            set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
            set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
        endif()
    endif()
    
    if(USE_OpenMP)
        find_package(OpenMP REQUIRED)
    endif(USE_OpenMP)
    
    if (OPENMP_FOUND)
        include_directories("${OPENMP_INCLUDES}")
        link_directories("${OPENMP_LIBRARIES}")
        set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
        set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
        # set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
    endif(OPENMP_FOUND)
    
    
    include_directories( ${OpenCV_INCLUDE_DIRS} )
    target_link_libraries( MyOpenCVTest ${OpenCV_LIBS})
    TARGET_LINK_LIBRARIES(MyOpenCVTest opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
    

    You might want to set e.g. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread") such that the linker automatically detects the appropriate pthread library

    Do

    brew reinstall llvm

    to install llvm compiler. Please note that, you can't use gcc compiler on Mac-Sierra for your project which needs openMP and also openCV. You need to use llvm compiler.

    Verify the correct location of llvm and openCV installation directory.

提交回复
热议问题