在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部分不在此问题的范围内,仅供参考,以说明我为什么需要从命令行运行它).我可以使用不满意的其他编译器来编译该项目:
I have a small test project that uses OpenMP for parallelization. My target is to compile it so it produces .dll
and .lib
for libraries (because my real project links to external libraries distributed using these types), with support for OpenMP 4.5 or newer and do it from the command line so it can be done on a docker for testing and checking (the docker part is out of the scope of this question, it is just for reference on why I need it to work from the command line). I can compile this project with different compilers with which I am not happy:
- MSVC:
mkdir build-msvc
,然后是cmake ..
(来自新创建的文件夹),最后是cmake --build . --config Release
.可以很好地编译,但是仅支持OpenMP 2.0,因此对于我的真实项目不是一个好的选择. - Intel Parallel Studio:
mkdir build-intel
,然后是cmake .. -T "Intel C++ Compiler 19.0"
(来自新创建的文件夹),最后是cmake --build . --config Release
.它支持OpenMP 5.0,但其许可证对我而言却非常昂贵. - MinGW x64 g ++:
mkdir build-g++
,然后是cmake .. -G "MinGW Makefiles"
(来自新创建的文件夹),最后是cmake --build .
.它支持OpenMP 4.5,但此编译器与.lib
(据我所知)不兼容,而我已经提到.lib
对我来说是必需的.
- MSVC:
mkdir build-msvc
, thencmake ..
(from the newly created folder) and finallycmake --build . --config Release
. This compiles fine but only supports OpenMP 2.0, therefore is not a good option for my real project. - Intel Parallel Studio:
mkdir build-intel
, thencmake .. -T "Intel C++ Compiler 19.0"
(from the newly created folder) and finallycmake --build . --config Release
. This supports OpenMP 5.0 but its licenses are pretty expensive for me. - MinGW x64 g++:
mkdir build-g++
, thencmake .. -G "MinGW Makefiles"
(from the newly created folder) and finallycmake --build .
. It supports OpenMP 4.5 but this compiler is not compatible wiht.lib
(as far as I know) which I already mentioned is necessary for me.
我尝试使用clang失败,
I have tried without success to use clang:
-
从MSVC中
- CLANG:
mkdir build-clang-msvc
,然后是cmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..
(来自新创建的文件夹),但是失败并显示以下错误:
- CLANG from MSVC:
mkdir build-clang-msvc
, thencmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..
(from the newly created folder), but it fails with the following error:
-- 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),但失败并出现以下错误: - Clang and Ninja apart from MSVC:
mkdir build-clang-ninja
, thencmake -G Ninja -DCMAKE_CXX_COMPILER=clang-cl ..
(from the newly created folder and changing thePATH
environmental variable so the non-msvc are found first), but it fails with the following error:
-- 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
的测试程序).
Any ideas on how to proceed with the clang? I think it is my best option for what I want to achieve (compile my test program with support for OpenMP 4.5 or newer and producing .lib
and .dll
).
我检查过但对解决此问题无用的相关帖子/网页:
Related posts/webpages that I have checked but have been non-useful to solve this:
- 在Windows上使用CMake,Clang构建c ++项目和忍者
- 在Windows上使用CMake,Ninja和Clang进行构建
- 在Windows上使用Clang进行编译的方式,专门设置链接器:
-DCMAKE_LINKER="C:/Program Files/LLVM/bin/lld-link.exe"
- 使用Ninja为Windows(clang-cl)构建Cake生成器
- Clang-cl用户手册
- Building c++ project on Windows with CMake, Clang and Ninja
- Building with CMake, Ninja and Clang on Windows
- Ways to Compile with Clang on Windows, specifically setting the linker:
-DCMAKE_LINKER="C:/Program Files/LLVM/bin/lld-link.exe"
- CMake building for Windows (clang-cl) using Ninja Generator
- Clang-cl user manual
推荐答案
对于 Clang + MSVC ,我能够复制此错误.因为我们试图从Visual Studio外部(即命令行)使用Visual Studio定制的编译器,所以在使用编译器之前,有必要在命令行中初始化 VS构建环境. VCVarsXX.bat
文件可完成此操作;它们是VS Command Prompt工具的一部分.因此,通过选择架构(x86
,x64
等)并运行脚本,这将使CMake使用clang-cl
构建简单的测试程序并继续.这是VS 2019的位置:
I was able to duplicate this error for the Clang+MSVC case. Because we are attempting to use the Visual-Studio-tailored compiler from outside Visual Studio (i.e. the command line), it is necessary to initialize the VS build environment in our command line before using the compiler. The VCVarsXX.bat
files accomplish this; they are part of the VS Command Prompt tools. So by picking your architecture (x86
, x64
, etc) and running the script, this should allow CMake to build the simple test program with clang-cl
and proceed. Here is where it is located for 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错误:
To expand on the OpenMP issue, I personally found using find_package(OpenMP REQUIRED)
to be pretty fruitless. As you mentioned, I also received this CMake error:
Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
此网站上有一些建议(例如此处)声称,手动填充所有变量允许
There are a few suggestions on this site (for example here and here) claiming that populating all of the variables manually allows FindOpenMP.cmake
to successfully find the libraries. I tried this with your example CMake file, with some success:
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
错误):
This succeeded in "finding" the libraries on the second CMake configuration attempt (the first always yielded the same Could NOT find OpenMP_CXX
error as before):
>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问题网站在此提出了CMake FindOpenMP.cmake
的缺点. ,并且在LLVM/Clang端也似乎存在相关限制.
However, this failed to build the executable, as none of the CMake OpenMP variables (particularly OpenMP_CXX_LIBRARIES
) actually point to the library location. The shortcomings of CMake's FindOpenMP.cmake
have been raised on the CMake issue site here, and there appear to be relevant limitations on the LLVM/Clang end as well.
无论如何,我能够使示例工作正常进行的最干净的方法是完全放弃find_package()
.以下内容使我能够成功生成生成系统,并编译并运行可执行文件:
Regardless, the cleanest way I was able to get the example working was to ditch find_package()
altogether. The following allowed me to successfully generate the buildsystem, and compile and run the executable:
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)
这篇关于在Windows中配置CMake以使用命令行中的clang获得最新的OpenMP支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!