如何与 OpenMP 在 MSVC 2015 上合作

How to get clang with OpenMP working on MSVC 2015

本文关键字:2015 MSVC OpenMP      更新时间:2023-10-16

我尝试让clang 5.0.0为Visual Studio 2015工作,因为我需要OpenMP 3.0功能。我安装了 clang 编译器(不是没有任何 openmp 支持的 vs2015 版本(并使用 cmake:

cmake_minimum_required(VERSION 2.8.10)
project(myproject)
find_package(OpenMP)
if (OPENMP_FOUND)
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
include_directories("include")
add_library(libFoo STATIC Foo.cpp)
install(TARGETS Foo libFoo LIBRARY DESTINATION lib ARCHIVE DESTINATION lib)

当我现在尝试配置带有或不带工具链的MSVC 14 2015 Win64构建时LLVM-vs2014我总是收到一个错误,即找不到 OpenMP:

The C compiler identification is Clang 5.0.0
The CXX compiler identification is Clang 5.0.0
Check for working C compiler: D:/Program Files/LLVM/msbuild-bin/cl.exe
Check for working C compiler: D:/Program Files/LLVM/msbuild-bin/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: D:/Program Files/LLVM/msbuild-bin/cl.exe
Check for working CXX compiler: D:/Program Files/LLVM/msbuild-bin/cl.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
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")
Configuring done

使用的编译器似乎是正确的(已安装的 clang,而不是Microsoft版本(,它会自动检测 clang-cl 二进制文件,但 OpenMP 失败。我尝试使用"指定本机编译器"手动指定编译器并获得相同的结果。它甚至选择了 clang-cl 版本而不是 clang++。

相关答案,这不能解决问题:

  • 使用 clang-cl 使用 openmp 编译 C 代码 - 最近的 clang 包含 libomp.lib 和 libiomp5.dll

这真的有点棘手,cmake 自动检测似乎效果不佳。有帮助的是

OpenMP_CXX_FLAGS="-Xclang -fopenmp"
OpenMP_C_FLAGS="-Xclang -fopenmp"

并确保libomp.lib在链接库中。

-Xclang告诉 clang 二进制文件,以下选项是 clang 格式而不是 MSVC 格式,而 -fopenmp 只是通常的 OpenMP 标志。但是,以任何其他方式设置它都不起作用。

一般模式为:-Xclang -clangFlag1 -Xclang -clangFlag2...。也就是说,每个 Clang 样式标志都需要自己的-Xclang