MacOS,CMake和OpenMP [英] MacOS, CMake and OpenMP
问题描述
我正在使用Homebrew的最新CMake(3.9.3)以及Brew的LLVM 5.0.0,因为这里的Clang具有OpenMP支持.
I am using the newest CMake (3.9.3) from Homebrew along with LLVM 5.0.0 also from Brew, because Clang here has OpenMP support.
这在带有LLVM 5的CMake 3.8.2中有效.
This worked in CMake 3.8.2 with LLVM 5.
我的CMakeLists.txt
中有
find_package( OpenMP )
后来我想做
if( OpenMP_CXX_FOUND )
但是,CMake似乎并没有采用find_package
指令.
However CMake doesn't seem to pick up on the find_package
directive.
我与一起运行CMake
I run CMake with
cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DUSE_WERROR=ON
我已经检查了clang
和clang++
正确指向/usr/local/opt/llvm/bin/clang
和/usr/local/opt/llvm/bin/clang++
where I have checked that clang
and clang++
points correctly to /usr/local/opt/llvm/bin/clang
and /usr/local/opt/llvm/bin/clang++
我得到的是以下两行:
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES) (found version "1.0")
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES) (found version "1.0")
如果我将自己设置为OpenMP_C_FLAGS
(使用-DOpenMP_C_FLAGS=-fopenmp=libomp
),则会将错误更改为
If I set OpenMP_C_FLAGS
myself (with -DOpenMP_C_FLAGS=-fopenmp=libomp
) it changes the error to
-- Could NOT find OpenMP_C (missing: OpenMP_C_LIB_NAMES) (found version "3.1")
请注意,它更改了版本号,所以它一定在找东西,对吧?
Notice that it changes the version number, so it must be finding something, right?
要使它正常工作,我缺少什么?
What am I missing for this to work properly?
好吧,似乎在CMake提供的FindOpenMP.cmake
中,我们执行了try_compile
,它默默地失败了(因为我们这样做了很多次,并且大多数都会失败,这很有意义).但是,使用Clang提供了-Werror
标志,该标志由于未使用命令行参数而失败.因此,我可以添加:
Okay, it seem that inside the FindOpenMP.cmake
supplied by CMake we do a try_compile
, which fails silently (because we do it a lot of times and most of them will fail, this makes sense). However, with Clang a -Werror
flag is supplied, which fails because of an unused command line argument. I can thus add:
if(APPLE)
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(OpenMP_C_FLAG "-fopenmp=libomp -Wno-unused-command-line-argument")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(OpenMP_CXX_FLAG "-fopenmp=libomp -Wno-unused-command-line-argument")
endif()
endif()
进入我的项目,因为我知道-fopenmp=libomp
将适用于此Clang.
to my project because I know that -fopenmp=libomp
will work for this Clang.
这是正确的方法吗?
推荐答案
该消息基本上告诉您,您必须提供库的路径和库的名称.以下示例可以解决您的问题(另请参见 find_package(OpenMP)).请注意,我通过命令"brew install llvm"使用brew安装.前四行只是为了完整性.
The message basically tells you that you have to provide the path to the libraries and the names of the libraries. The following example should fix your problem (see also find_package(OpenMP)). Note that I use the brew installation using the command "brew install llvm". The first four lines are just for completeness.
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")
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)
您可能想要设置例如set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
,以便链接程序自动检测适当的pthread库(请参阅 Wiki ).
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 (see pthread and wiki).
这篇关于MacOS,CMake和OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!