gpt4 book ai didi

c++ - 在 Windows 中配置 CMake 以从命令行使用 clang 以获得现代 OpenMP 支持

转载 作者:行者123 更新时间:2023-12-01 22:39:42 26 4
gpt4 key购买 nike

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

  • MSVC:mkdir build-msvc,然后 cmake ..(来自新创建的文件夹),最后 cmake --build 。 --config 发布。这编译得很好,但只支持 OpenMP 2.0,因此对于我的实际项目来说不是一个好的选择。
  • Intel Parallel Studio:mkdir build-intel,然后 cmake .. -T "Intel C++ Compiler 19.0"(来自新创建的文件夹),最后 cmake --build 。 --config 发布。它支持 OpenMP 5.0,但它的许可证对我来说相当昂贵。
  • MinGW x64 g++:mkdir build-g++,然后 cmake .. -G "MinGW Makefiles"(来自新创建的文件夹),最后 cmake - -构建。。它支持 OpenMP 4.5,但该编译器与 .lib 不兼容(据我所知),我已经提到过这对我来说是必要的。

我尝试使用 clang 但没有成功:

  • 来自 MSVC 的 CLANG:mkdir build-clang-msvc,然后 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 -- brokenCMake 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".
  • Clang 和 Ninja 除了 MSVC: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 -- brokenCMake 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".

关于如何继续 clang 有什么想法吗?我认为这是我想要实现的最佳选择(编译我的测试程序并支持 OpenMP 4.5 或更高版本并生成 .lib.dll)。

我已检查过但对解决此问题无济于事的相关帖子/网页:

最佳答案

我能够在 Clang+MSVC 案例中重复此错误。由于我们尝试从 Visual Studio 外部(即命令行)使用 Visual-Studio 定制的编译器,因此有必要在使用编译器之前在命令行中初始化 VS 构建环境。 VCVarsXX.bat 文件完成此任务;它们是 VS 命令提示符工具的一部分。因此,通过选择您的架构( x86x64 等)并运行脚本,这应该允许 CMake 使用 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 问题,我个人发现使用 find_package(OpenMP REQUIRED)毫无成果。正如您提到的,我也收到了这个 CMake 错误:

Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)

此网站上有一些建议(例如 herehere ),声称手动填充所有变量允许 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)

这在第二次 CMake 配置尝试中成功“找到”了库(第一次总是产生与之前相同的 Could NOT find 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_CXX_LIBRARIES )实际指向库位置。 CMake的缺点FindOpenMP.cmake已在 CMake 问题网站 here 上提出,并且 LLVM/Clang 端似乎也存在相关限制。

<小时/>

无论如何,我能够让示例正常工作的最干净的方法是放弃 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)

关于c++ - 在 Windows 中配置 CMake 以从命令行使用 clang 以获得现代 OpenMP 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58285259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com