David S.
David S.

Reputation: 128

CMake Support for OpenMP on macOS High Sierra

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:

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}")
    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} 
${OpenMP_EXE_LINKER_FLAGS}")
endif()

Unfortunately this doesn't seem to work on macOS targets. When cmake is called, the following error is given:

-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS) 
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS) 
-- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND) 

I'm using macOS High Sierra (10.13.3) along with CMake 3.11. I've installed OpenMP 5.01 via brew, 'brew install libomp'. I've found some previous posts commenting on issues regarding these three but they all seem to deal with a previous way of installing OpenMP on macOS, "brew install clang-omp".

I'm thinking this might have something to do with CMake not support this OpenMP install as I'm able to use OpenMP no problem with standard makefiles. Any information provided would be much appreciated.

Upvotes: 3

Views: 3744

Answers (2)

Tanmoy Mondal
Tanmoy Mondal

Reputation: 449

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 <omp.h>" 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.

Upvotes: 3

David S.
David S.

Reputation: 128

I've been able to answer my own question (apologies for not figuring this out beforehand, hopefully this can help others with the same issue).

It seems that a patch has been submitted to CMake to allow it to properly create buildsystems with the new OpenMP install: https://gitlab.kitware.com/cmake/cmake/merge_requests/1812

For others seeing this in the future, update to CMake 3.12 if it has been released at the time of reading.

Upvotes: 4

Related Questions