Cmake 不适用于 Maverick 上的 openMP

Posted

技术标签:

【中文标题】Cmake 不适用于 Maverick 上的 openMP【英文标题】:Cmake does not work with openMP on Maverick 【发布时间】:2016-03-16 22:56:52 【问题描述】:

我正在使用 OpenMP 编写并行代码。 这是我案例中使用的 CMakeLists 的一部分。

FIND_PACKAGE( OpenMP REQUIRED)
    if(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $OpenMP_CXX_FLAGS")
    set(CMAKE_EXE_LINKER_FLAGS "$CMAKE_EXE_LINKER_FLAGS $OpenMP_EXE_LINKER_FLAGS")
    endif()

但是,当我们启动 cmake .. 时,它似乎找不到 OpenMP 包,我得到了这个错误

Try OpenMP C flag = [ ]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [/openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-Qopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-xopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [+Oopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-qsmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP C flag = [-mp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [ ]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [/openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-Qopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-openmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-xopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [+Oopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-qsmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
-- Try OpenMP CXX flag = [-mp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Failed
CMake Error at /opt/local/share/cmake-3.3/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
  Could NOT find OpenMP (missing: OpenMP_C_FLAGS OpenMP_CXX_FLAGS)

这里是 CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

#Declaration du projet

project(MYfirstcamke)

set(EXECUTABLE_OUTPUT_PATH bin/$CMAKE_BUILD_TYPE)

#Inclusion de opencv

include_directories(/usr/local/include/)

link_directories(/usr/local/lib)

FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS $OpenMP_CXX_FLAGS")
set(CMAKE_EXE_LINKER_FLAGS "$CMAKE_EXE_LINKER_FLAGS $OpenMP_EXE_LINKER_FLAGS")
endif()

find_package(PCL 1.2 REQUIRED)
include_directories($PCL_INCLUDE_DIRS)
link_directories($PCL_LIBRARY_DIRS)
add_definitions($PCL_DEFINITIONS)

file(

    GLOB_RECURSE

    source_files

    src/*


)
#declaration de l'executable

add_executable(

my_executable

$source_files

)
#Configuration de l'edition de lien.
target_link_libraries(

my_executable opencv_core  opencv_imgproc opencv_video opencv_highgui $PCL_LIBRARIES


)

我该如何解决这个问题?我确定 openMPi 已安装并且它位于 opt/local/include

【问题讨论】:

你确定你没有混淆 OpenMP 和 OpenMPI 吗?我看到您使用了这两个标签,并且您在帖子末尾引用了 OpenMPI。但两者完全不同。 OpenMP 是多线程并行语言扩展的标准,OpenMPI 是消息传递的 MPI 标准的实现。你喜欢这两个中的哪一个? OpenMP 不是一个包,而是编译器的一个组成部分。 Xcode 很久以前就提供了一个支持 OpenMP 的 GCC。然后 Apple 切换到不支持 OpenMP 的 Clang,至少在 Xcode 附带的版本中不支持。您应该从源代码或使用 Homebrew/MacPorts/whatever 安装 GCC。 @Gilles,可能是我在 OpenMP 和 OpenMPI 之间进行混合。我认为 OpenMP 已被弃用并成为 OpenMP。 @HristoIliev,我已经安装了 GCC,但是如何在 CMakeLists.txt 中指定编译器。我在我的问题中添加了 CMakeLists.txt 文件。 我不懂 CMake,但在运行 CMake 之前可能像 export CC=gcc-4.9; export CXX=g++-4.9 这样的东西就足够了。将gxx-4.9替换为对应编译器的实际名称。 【参考方案1】:

OpenMP 和 MPI 是两个完全不同的东西。

OMP 需要编译器支持,在 OS X 上,您需要通过 Homebrew 提供。 (你也可以通过 Homebrew 安装 MPI,这很好)。

brew install clang-omp

安装 clang-omp 后,您可以通过像这样调用它来告诉 CMake 使用该编译器。

CC=clang-omp CXX=clang-omp++ cmake <insert your normal cmake parameters here>

【讨论】:

根据openmp.llvm.org, > "随着 Clang 3.8.0 的发布,OpenMP 3.1 支持默认在 Clang 中启用,因此 OpenMP 运行时构建为 Clang 构建的正常部分” 我就是不明白为什么我还要安装“clang-omp”,你能解释一下为什么吗?另一个问题是clang-omp 公式要求与llvm 公式不同版本的clang(和compiler-rt 等),那么我应该怎么做才能使事情正确呢? 我正在尝试使用 CMake 3.7 和 clang 4.0,但我的 find_package(OpenMP REQUIRED) 失败了。【参考方案2】:

除了上面的答案,你还可以从 homebrew 安装 gcc-5/g++-5。普通的 gcc-5 安装(没有 Clang 包装器)将支持 OpenMP,这应该由 CMake 通过 Find_Package 检测到。

【讨论】:

【参考方案3】:

一种解决方案是安装和使用 GNU GCC。假设您已经安装或将通过 HomeBrew 安装它brew install gcc

if(APPLE)
  set(CMAKE_C_COMPILER "/usr/local/bin/gcc-9")
  set(CMAKE_CXX_COMPILER "/usr/local/bin/g++-9")
  set(CMAKE_Fortran_COMPILER "/usr/local/bin/gfortran")
endif()

-9 版本可能会在当时发生变化。

【讨论】:

以上是关于Cmake 不适用于 Maverick 上的 openMP的主要内容,如果未能解决你的问题,请参考以下文章

cmake:为啥 CMAKE_CXX_STANDARD 似乎不适用于 check_cxx_source_compiles

CMake find_package 不适用于本征?

cmake find_path 其中路径包含版本号

调试不适用于 Android Studio 的 C++/本机库模块(使用 Cmake)

异常不适用于 arm cortex m4 的 clang 和 cmake 交叉编译,并生成“got”部分

Xcode自动补全不适用于通过CMake包含的C ++库