当前位置: 首页 > 知识库问答 >
问题:

在windows中配置CMake以从命令行使用clang来获得现代OpenMP支持

华章横
2023-03-14

我有一个使用 OpenMP 进行并行化的小型测试项目。我的目标是编译它,以便它为库生成 .dll.lib(因为我的真实项目链接到使用这些类型分发的外部库),支持 OpenMP 4.5 或更高版本,并从命令行执行此操作,以便可以在 docker 上完成进行测试和检查(docker 部分超出了这个问题的范围, 它只是为了参考为什么我需要它从命令行工作)。我可以使用我不满意的不同编译器编译这个项目:

    < Li > MSVC:< code > mkdir build-msvc ,然后< code>cmake..(来自新创建的文件夹),最后< code>cmake - build。-配置版本。这可以很好地编译,但是只支持OpenMP 2.0,因此对我的实际项目来说不是一个好的选择。 < li >英特尔Parallel Studio:< code > mkdir build-Intel ,然后< code>cmake..-T "英特尔C编译器19.0"(从新创建的文件夹中),最后是< code>cmake - build。-配置版本。这支持OpenMP 5.0,但它的许可证对我来说相当昂贵。 < Li > MinGW x64 g:< code > mkdir build-g ,然后< code>cmake..-G "MinGW Makefiles"(来自新创建的文件夹),最后是< code>cmake - build。。它支持OpenMP 4.5,但该编译器与< code >不兼容。lib(据我所知)我已经提到的对我来说是必要的。

我试过用铿锵声,但没有成功:

    < li >来自MSVC的CLANG:< code > mkdir build-CLANG-msvc ,然后< code > cmake-G Ninja-DCMAKE _ CXX _ COMPILER = CLANG-cl..(从新创建的文件夹中),但它失败并显示以下错误:
-- The CXX compiler identification is Clang 8.0.1 with MSVC-like command-line
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.15/Modules/CMakeTestCXXCompiler.cmake:53 (message):
  The C++ compiler

    "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/[USER NAME]/source/repos/test_openmp/build-clang-msvc/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/PROGRA~2/MICROS~1/2019/COMMUN~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe cmTC_bd131 && [1/2] Building CXX object CMakeFiles\cmTC_bd131.dir\testCXXCompiler.cxx.obj
    [2/2] Linking CXX executable cmTC_bd131.exe
    FAILED: cmTC_bd131.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_bd131.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests  -- C:\PROGRA~1\MINGW-~1\X86_64~1.0-P\mingw64\bin\ld.exe /nologo CMakeFiles\cmTC_bd131.dir\testCXXCompiler.cxx.obj  /out:cmTC_bd131.exe /implib:cmTC_bd131.lib /pdb:cmTC_bd131.pdb /version:0.0  /machine:X86  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    RC Pass 1: command "rc /fo CMakeFiles\cmTC_bd131.dir/manifest.res CMakeFiles\cmTC_bd131.dir/manifest.rc" failed (exit code 0) with the following output:
    The system cannot find the file specified
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "C:/Users/[USER NAME]/source/repos/test_openmp/build-clang-msvc/CMakeFiles/CMakeOutput.log".
See also "C:/Users/[USER NAME]/source/repos/test_openmp/build-clang-msvc/CMakeFiles/CMakeError.log".
  • 除了 MSVC 之外的 Clang 和 Ninja:mkdir build-clang-ninja,然后 cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..(从新创建的文件夹并更改 PATH 环境变量以便首先找到非 msvc),但它失败并显示以下错误:
-- The CXX compiler identification is Clang 9.0.0 with MSVC-like command-line
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.15/Modules/CMakeTestCXXCompiler.cmake:53 (message):
  The C++ compiler

    "C:/Program Files/LLVM/bin/clang-cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/Users/[USER NAME]/source/repos/test_openmp/buid-clang-ninja/CMakeFiles/CMakeTmp

    Run Build Command(s):C:/Ninja/ninja.exe cmTC_50b73 && [1/2] Building CXX object CMakeFiles\cmTC_50b73.dir\testCXXCompiler.cxx.obj
    [2/2] Linking CXX executable cmTC_50b73.exe
    FAILED: cmTC_50b73.exe
    cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_50b73.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests  -- CMAKE_LINKER-NOTFOUND /nologo CMakeFiles\cmTC_50b73.dir\testCXXCompiler.cxx.obj  /out:cmTC_50b73.exe /implib:cmTC_50b73.lib /pdb:cmTC_50b73.pdb /version:0.0  /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
    RC Pass 1: command "rc /fo CMakeFiles\cmTC_50b73.dir/manifest.res CMakeFiles\cmTC_50b73.dir/manifest.rc" failed (exit code 0) with the following output:
    The system cannot find the file specified
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "C:/Users/[USER NAME]/source/repos/test_openmp/buid-clang-ninja/CMakeFiles/CMakeOutput.log".
See also "C:/Users/[USER NAME]/source/repos/test_openmp/buid-clang-ninja/CMakeFiles/CMakeError.log".

关于如何进行叮当声的任何想法?我认为这是我想要实现的目标的最佳选择(编译我的测试程序,支持 OpenMP 4.5 或更高版本并生成 .lib.dll)。

我查看过的相关帖子/网页,但对解决这个问题没有帮助:

  • 在Windows上使用Cake、Clang和Ninja构建c项目
  • 在Windows上使用Cake、Ninja和Clang构建
  • Windows上使用Clang编译的方法,特别是设置链接器:-DCMAKE_LINKER="C:/Program Files/LLVM/bin/lld-link.exe"
  • 使用忍者生成器为Windows(clang-cl)构建
  • Clang-cl用户手册

共有1个答案

商夜洛
2023-03-14

我能够为Clang MSVC案例复制此错误。因为我们正尝试从Visual Studio外部(即命令行)使用Visual Studio定制的编译器,所以在使用编译器之前,有必要在我们的命令行中初始化VS构建环境。VCVarsXX.bat文件完成了这一点;它们是VS命令提示符工具的一部分。因此,通过选择您的架构(x86x64等)并运行脚本,这应该允许Cake使用clang-cl构建简单的测试程序并继续。这是VS 2019的位置:

>"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" x86

>cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..
-- The CXX compiler identification is Clang 8.0.1
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/bin/clang-cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
...

扩展一下OpenMP的问题,我个人发现使用< code > find _ package(OpenMP REQUIRED)是没有用的。正如您提到的,我也收到了这个CMake错误:

Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)

在这个网站上有一些建议(例如这里和这里),声称手动填充所有变量可以让FindOpenMP.cmake成功找到库。我用你的示例 CMake 文件尝试了这个,取得了一些成功:

cmake_minimum_required (VERSION 2.8)
project(test_openmp LANGUAGES CXX)

set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-Xclang -fopenmp")
set(OpenMP_CXX_LIB_NAMES "libomp" "libiomp5")
set(OpenMP_libomp_LIBRARY libomp)
set(OpenMP_libiomp5_LIBRARY libiomp5)

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

# Library
add_library(example_lib SHARED example_lib.h example_lib.cpp)
target_link_libraries(example_lib PUBLIC ${OpenMP_CXX_LIBRARIES})
target_compile_definitions(example_lib PRIVATE EXEMPLE_LIB_EXPORT)

# Executable
add_executable(test_openmp test_openmp.cpp)
target_link_libraries(test_openmp example_lib)

这成功地“找到”了第二次配置尝试时的库(第一次总是产生与之前相同的找不到OpenMP_CXX错误):

>cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP: TRUE (found version "3.1")
-- Configuring done
-- Generating done

但是,这无法生成可执行文件,因为CMake OpenMP变量(尤其是OpenMP_CX_LIBRARIES)实际上都没有指向库位置。CMake的<code>FindOpenMP的缺点。cmake</code>已经在这里的cmake问题网站上提出,并且LLVM/Clang端似乎也存在相关限制。

不管怎样,我能让这个例子工作的最干净的方法是完全抛弃< code>find_package()。以下代码允许我成功地生成构建系统,并编译和运行可执行文件:

cmake_minimum_required (VERSION 2.8)
project(test_openmp LANGUAGES CXX)

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(example_lib SHARED example_lib.h example_lib.cpp)
# Link in the OpenMP libraries.
target_link_libraries(example_lib PUBLIC libomp libiomp5md)
target_compile_definitions(example_lib PRIVATE EXEMPLE_LIB_EXPORT)

# Executable
add_executable(test_openmp test_openmp.cpp)
target_link_libraries(test_openmp example_lib)
 类似资料:
  • 问题内容: 我想运行安装游览,但是找不到使用代理访问互联网的选项。我不仅仅为了旅游而需要它,而是为了总体上在Go中进行开发。 如何配置Go以使用代理。 问题答案: Go程序了解环境变量和,但这还不够,因为使用源代码管理器来检索代码。因此,您还必须为SCM设置HTTP代理设置。使用此为水银和这个 GIT中。 价值可以像。用户,密码和端口部分是可选的。是不应该通过代理服务器连接的服务器的逗号分隔列表。

  • 问题内容: npm 已安装并正在IntelliJ IDEA 15中被积极使用 我的目标是在IntelliJ中为我的TypeScript源代码生成类型,但是我想学习使用Windows命令行,因此我可以显式指定命令行选项,以了解每个选项的作用。我对Googling所发现的与设置和使用它有关的各种花絮感到困惑…我敢肯定,我缺少一些非常基本的知识,那些博客或回答问题的人都认为这是常识。 。 这是我尝试过的

  • 问题内容: 我的Maven设置文件 〜/ .m2 / settings.xml中* 默认激活了 一个配置文件。 * 是否可以通过执行以下操作从命令行停用它: 问题答案: 是的,的确,您有正确的方法。从Maven配置文件用户指南 停用个人资料 从Maven 2.0.10开始,可以使用命令行停用一个或多个配置文件,方法是在其标识符前面加上字符’!’。或“-”,如下所示: 这可用于停用标记为active

  • 我正在我的电脑上运行Jenkins作为Windows服务。我正在努力学习使用它。对于虚拟作业,构建步骤之一是使用以下命令执行Windows批处理命令 当我在Jenkins上构建作业时,我得到一个构建错误,即JLink不被识别为内部或外部命令、可操作程序或批处理文件。当我从命令提示符下运行命令时,它运行起来没有任何问题。Windows路径已设置为安装JLink的路径。我应该在Jenkins中另外配置

  • 我已经检查了相关的线程-如何在配置单元脚本中设置变量 在hive内部,变量运行良好: 错误:java.lang.IllegalArgumentException:无法从空字符串创建路径:位于org.apache.hadoop.fs.Path.CheckPathArg(Path.java:131)(位于org.apache.hadoop.fs.Path.(Path.java:139)(位于org.a

  • 问题内容: 我已经安装了最新版本的Boost (包含in 和库中的),现在尝试从源代码安装Wt,但是CMake(2.6版)似乎找不到Boost的安装。它试图提供有关设置BOOST_DIR和Boost_LIBRARYDIR的有用建议,但是我无法通过调整这些变量来使其正常工作。 我收到的最新错误消息是它找不到库,但似乎表明它对包含路径使用“ / usr / local / include”,这是不正确