簡體   English   中英

在 windows 中配置 CMake 以使用 clang 從命令行獲得現代 OpenMP 支持

[英]Configure CMake in windows to use clang from command line to get modern OpenMP support

我有一個使用 OpenMP 進行並行化的小型測試項目 我的目標是編譯它,以便它為庫生成.dll.lib (因為我的真實項目鏈接到使用這些類型分發的外部庫),支持 OpenMP 4.5 或更高版本,並從命令行執行,因此可以完成在 docker 上進行測試和檢查(docker 部分不在此問題的 scope 中,僅供參考,說明為什么我需要它從命令行工作)。 我可以用我不滿意的不同編譯器編譯這個項目:

  • MSVC: mkdir build-msvc ,然后cmake.. (來自新創建的文件夾),最后cmake --build. --config Release cmake --build. --config Release 這編譯得很好,但只支持 OpenMP 2.0,因此對於我的實際項目來說不是一個好的選擇。
  • Intel Parallel Studio: mkdir build-intel ,然后cmake.. -T "Intel C++ Compiler 19.0" (來自新創建的文件夾),最后cmake --build. --config Release cmake --build. --config Release 這支持 OpenMP 5.0,但它的許可證對我來說相當昂貴。
  • MinGW x64 g++: mkdir build-g++ ,然后cmake.. -G "MinGW Makefiles" (來自新創建的文件夾),最后cmake --build. . 它支持 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 -- 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".
  • 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 -- 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".

關於如何繼續使用 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)

此站點上有一些建議(例如此處此處)聲稱手動填充所有變量允許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問題網站上提出,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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM