缺少:OpenMP_C_FLAGS OpenMP_C_LIB_NAMES

问题描述 投票:0回答:6

我在 Mac OSX 上使用 OpenMP 编译项目时遇到了困难。 错误是:

CMake Error at /usr/local/Cellar/cmake/3.10.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES)
Call stack most recent call first) 
/usr/local/Cellar/cmake/3.10.2/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
/usr/local/Cellar/cmake/3.10.2/share/cmake/Modules/FindOpenMP.cmake:447 (find_package_handle_standard_args)
libRORPO/CMakeLists.txt:7 (find_package)

项目中与 OpenMP 搜索相关的 CMakeLists 文件如下:

# RORPO Lib

project(libRORPO)
cmake_minimum_required(VERSION 2.8)

# FIND OPENMP
find_package( OpenMP REQUIRED)
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()

# ADD FILES
file(GLOB_RECURSE RORPO_HEADERS *.hpp *.h)
file(GLOB_RECURSE RORPO_SOURCES *.c)

add_library(RORPO ${LIB_TYPE} ${RORPO_SOURCES} ${RORPO_HEADERS})

install( FILES ${RORPO_HEADERS} DESTINATION include/libRORPO)
install( TARGETS RORPO DESTINATION lib)

我使用 gcc-7 和 Clang-omp 验证了 OpenMP 安装是否运行良好。我搜索过类似的错误。响应指出,cmake 中有一个最新的包允许查找 OpenMp。我重新安装了 CMAKE 以获取最新版本,但它仍然无法工作。 我不是 cmake 配置方面的专家...如果您能指导我,我将不胜感激。

c++ macos cmake openmp
6个回答
12
投票

这是因为 Xcode clang 不支持,请使用系统 clang 代替。

方法一:设置环境变量,然后运行cmake https://gist.github.com/svenevs/f00ed3898d2af6248921b63254aa8cc1

export CC=/usr/local/opt/llvm/bin/clang
export CXX=/usr/local/opt/llvm/bin/clang++
export LDFLAGS="-L/usr/local/opt/llvm/lib"
export CPPFLAGS="-I/usr/local/opt/llvm/include"

方法2:在CMakeLists.txt中设置环境变量

message(STATUS "set env var")
set(ENV{CC} "/usr/local/opt/llvm/bin/clang")
set(ENV{CXX} "/usr/local/opt/llvm/bin/clang++")
set(ENV{LDFLAGS} "-L/usr/local/opt/llvm/lib")
set(ENV{CPPFLAGS} "-I/usr/local/opt/llvm/include")

方法3:更改CMakeLists.txt中的var

message(STATUS "set compipler")
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/5.0.1/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/5.0.1/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/5.0.1/include")

set(OPENMP_LIBRARIES "/usr/local/opt/llvm/lib")
link_directories(${OPENMP_LIBRARIES})

方法4.运行 cmake 时更改环境变量

cmake .. -DCC=xxx -DCXX=xxx -DLDFLAGS="" -DCPPFLAGS=""

7
投票

查找 OpenMP

尽管到目前为止还有其他答案,但可以使用系统编译器。

if(APPLE)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
    set(OpenMP_C "${CMAKE_C_COMPILER}" CACHE STRING "" FORCE)
    set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument" CACHE STRING "" FORCE)
    set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5" CACHE STRING "" FORCE)
    set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES} CACHE STRING "" FORCE)
    set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES} CACHE STRING "" FORCE)
    set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES} CACHE STRING "" FORCE)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  set(OpenMP_CXX "${CMAKE_CXX_COMPILER}" CACHE STRING "" FORCE)
  set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument" CACHE STRING "" FORCE)
  set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5" CACHE STRING "" FORCE)
  set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES} CACHE STRING "" FORCE)
  set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES} CACHE STRING "" FORCE)
  set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES} CACHE STRING "" FORCE)
endif()
endif()

5
投票

我在基于 ARM 的 MacOS 机器上遇到了这个问题,安装 libomp 修复了它:

arch -x86_64 brew install libomp

3
投票

我找到了一个解决方案:http://stechschulte.net/2016/03/20/openmp-osx-cmake.html

诀窍是 cmake 必须使用指向 clang-omp 的 CC 和 CXX 环境变量运行:

CC=clang-omp CXX=clang-omp++ cmake

请注意,在安装 llvm 后必须通过以下方式定义 clang-omp 和 clang-omp++:

brew install llvm

并创建两个符号链接:

ln -s /usr/local/opt/llvm/bin/clang /usr/local/bin/clang-omp
ln -s /usr/local/opt/llvm/bin/clang++ /usr/local/bin/clang-omp++

2
投票

使用@lbsweek提示:

在映射了brew安装llvm的位置后,它对我有用。

这是“/opt/homebrew/Cellar/llvm/{version}”

了解后,您可以将 OpenMP_C_FLAGS OpenMP_C_LIB_NAMES 映射为:

export CC=/opt/homebrew/Cellar/llvm/15.0.6/bin/clang
export CXX=/opt/homebrew/Cellar/llvm/15.0.6/bin/clang++
export LDFLAGS="-L/opt/homebrew/Cellar/llvm/15.0.6/lib"
export CPPFLAGS="-I/opt/homebrew/Cellar/llvm/15.0.6/include"

0
投票

需要在 macOS 上设置 OpenMP_ROOT 以指向 libomp。 对于 macOS,首先执行以下操作:

brew install libomp

并在~/.zshrc中设置

export OpenMP_ROOT=$(brew --prefix)/opt/libomp
© www.soinside.com 2019 - 2024. All rights reserved.