CMake 3.9对 OpenMP 的支持有了很大的提升,但是根据Modern CMake中的教程,如下配置可能不会生效:

find_package(OpenMP)
if(OpenMP_CXX_FOUND)
    target_link_libraries(MyTarget PUBLIC OpenMP::OpenMP_CXX)
endif()

而是可能出现如下提示:

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

解决办法为:

1. 先安装 libomp

brew install libomp

2. 如果安装后再运行CMake无错误提示,那就直接可以用。如果还是报同样的错误,则需要继续设置环境变量:

export OpenMP_ROOT=$(brew --prefix)/opt/libomp

参考

  1. How to find OpenMP with clang on macOS

2. OpenMP simple C example with CMake