在Visual Studio中将OpenMP与Clang和CMake一起使用 [英] Using OpenMP with Clang and CMake in Visual Studio
问题描述
我正在尝试编译一个简单的应用程序,以测试将来可能使用的一些库。由于msvc遇到了一些问题,我尝试了Clang,这使我消失了一个奇怪的错误。
I'm trying to compile a simple app to test a few libraries I might be using in the future. Because of some problems I had with msvc I tried Clang, which made a strange error I got disappear.
我现在遇到的问题是我要测试的库OpenMP。他们使用FindOpenMP模块CMake privides导入它。但是,该模块在Clang中找不到。
The problem I have now is that the libraries I want to test use OpenMP. They import it using the FindOpenMP module CMake privides. However the module doesn't find it with Clang.
cmake_minimum_required(VERSION 3.14.0)
project(blaze-test VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(OpenMP)
我得到了以下输出:
1> CMake generation started for configuration: 'x64-Debug'.
1> Environment settings:
1> CXXFLAGS=-m64 -fdiagnostics-absolute-paths
1> CFLAGS=-m64 -fdiagnostics-absolute-paths
1> Command line: "cmd.exe" /c ""C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_INSTALL_PREFIX:PATH="PATH\blaze-test\install\x64-Debug" -DCMAKE_CXX_COMPILER:FILEPATH="C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe" -DCMAKE_C_COMPILER:FILEPATH="C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe" -DCMAKE_BUILD_TYPE="Debug" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "PATH\blaze-test" 2>&1"
1> Working directory: PATH\blaze-test\build\x64-Debug
1> [CMake] -- The C compiler identification is Clang 8.0.0
1> [CMake] -- The CXX compiler identification is Clang 8.0.0
1> [CMake] -- Check for working C compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe
1> [CMake] -- Check for working C compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe -- works
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Check for working CXX compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe
1> [CMake] -- Check for working CXX compiler: C:/PROGRAM FILES (X86)/MICROSOFT VISUAL STUDIO/2019/COMMUNITY/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe -- works
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES)
1> [CMake] -- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
1> [CMake] -- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)
基于此我添加了
set(OpenMP_CXX_FLAGS "-Xclang -fopenmp" CACHE STRING "" FORCE)
set(OpenMP_C_FLAGS "-Xclang -fopenmp" CACHE STRING "" FORCE)
在find_package调用之前。它从错误消息中删除了有关编译器标志的部分。我以相同的方式添加了libomp.lib。然后我得到
right before the find_package call. It removed the part about the compiler flags from the error message. I added libomp.lib the same way. Then I get
1> [CMake] -- Could NOT find OpenMP_C (missing: OpenMP_C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/8.0.0/lib/libomp.lib_LIBRARY)
1> [CMake] -- Could NOT find OpenMP_CXX (missing: OpenMP_C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/8.0.0/lib/libomp.lib_LIBRARY)
1> [CMake] -- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)
我希望有人可以告诉我如何让CMake找到它。这些不可靠的查找模块真的很烦人。
I hope someone can tell me how to get CMake to find it. These unreliable Find Modules are really annoying.
ps:我在CMake 3.14和Visual Studio 2019中使用Clang 8(clang-cl.exe)。
ps: I'm using Clang 8 (clang-cl.exe) with CMake 3.14 and Visual Studio 2019.
推荐答案
使用 find_package(需要OpenMP)
时,使用时我也收到类似的错误Visual Studio中的 clang-cl
。已知的限制与CMake的 FindOpenMP.cmake
模块,以及 clang-cl
方面缺乏支持。此网站上还有其他答案,建议填充CMake OpenMP _
手动变量,但这似乎倒退了。当我尝试时,CMake声称 OpenMP
是 FOUND
,但是到库的路径仍然未知,并且编译是不成功。我能够成功运行CMake,然后通过避免使用 find_package
和(不幸的是)现在硬编码OpenMP库的路径,使用MSVC + Clang编译我的应用程序:
When using find_package(OpenMP REQUIRED)
, I have also received similar errors when using clang-cl
from Visual Studio. There are known limitations with CMake's FindOpenMP.cmake
module, as well as some lack of support the clang-cl
side. There are other answers on this site suggesting to populate the CMake OpenMP_
variables manually, but this seems backwards. When I tried it, CMake claimed that OpenMP
was FOUND
, but the paths to the libraries was still unknown and compilation was unsuccessful. I was able to successfully run CMake then compile my applications with MSVC+Clang by avoiding the use of find_package
and (unfortunately) hard-coding the path to the OpenMP libraries for now:
cmake_minimum_required(VERSION 3.14.0)
project(blaze-test VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(OpenMP_LIBRARY_DIR "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/lib")
set(OpenMP_CXX_FLAGS "-Xclang -fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# Tell CMake where to find the OpenMP libraries.
link_directories(${OpenMP_LIBRARY_DIR})
# Library
add_library(MyLibrary SHARED MyExample.h MyExample.cpp)
# Link in the OpenMP libraries.
target_link_libraries(MyLibrary PUBLIC libomp libiomp5md)
# Executable
add_executable(MyOpenMpTest MyOpenMpTest .cpp)
target_link_libraries(MyOpenMpTest MyLibrary)
希望CMake和LLVM的人们很快就会解决此问题,并且 FindOpenMP.cmake
可以更可靠地使用。
Hopefully, the folks at CMake and LLVM will get this issue worked out soon and FindOpenMP.cmake
can be used more reliably.
这篇关于在Visual Studio中将OpenMP与Clang和CMake一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!