'How to use Clang-CL LLVM with VS2019 CMAKE?
As of writing clang has this documentation on their user manual for Visual Studio:
cmake -G"Visual Studio 2017" -T LLVM ..
This doesn't work.
I get this mess of an error message from cmake:
CMake Error at CMakeLists.txt:3 (project):
Failed to run MSBuild command:
C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe
to get the value of VCTargetsPath:
Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 10/3/2020 5:06:06 PM.
Project "D:\Github\glfw\build\CMakeFiles\3.18.1\VCTargetsPath.vcxproj" on node 1 (default targets).
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(411,5): error MSB8020: The build tools for LLVM99 (Platform Toolset = 'LLVM99') cannot be found. To build using the LLVM99 build tools, please install LLVM99 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting "Retarget solution". [D:\Github\glfw\build\CMakeFiles\3.18.1\VCTargetsPath.vcxproj]
Done Building Project "D:\Github\glfw\build\CMakeFiles\3.18.1\VCTargetsPath.vcxproj" (default targets) -- FAILED.
Build FAILED.
"D:\Github\glfw\build\CMakeFiles\3.18.1\VCTargetsPath.vcxproj" (default target) (1) ->
(PrepareForBuild target) ->
on, and then selecting "Retarget solution". [D:\Github\glfw\build\CMakeFiles\3.18.1\VCTargetsPath.vcxproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.11
Exit code: 1
-- Configuring incomplete, errors occurred!
See also "D:/Github/glfw/build/CMakeFiles/CMakeOutput.log".
Solution 1:[1]
This doesn't work with VS2019. In vs2019 you MUST specify the toolset as ClangCL. This change isn't documented anywhere as far as I can see. I was only able to figure this out via trial and error.
cmake -G"Visual Studio 2019" -T ClangCL ..
Here is the output you'll get.
PS D:\Github\glfw> cmake -S . -B build -T ClangCl
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19041.
-- The C compiler identification is Clang 10.0.0 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Using Win32 for window creation
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Github/glfw/build
Here is what it will look like:
EDIT:
You can also set the toolset manually like this:
# Must be called before project call
set(CMAKE_GENERATOR_TOOLSET "ClangCL")
project(FOOBAR
LANGUAGES "CXX"
)
Solution 2:[2]
As mentionned in https://cmake.org/cmake/help/v3.18/manual/cmake.1.html
-T <toolset-spec>
Toolset specification for the generator, if supported.
Visual 2017 don't support Clang/LLVM:
https://docs.microsoft.com/cpp/build/clang-support-msbuild?view=vs-2017
For visual 2019 you need to use clang-cl
. https://docs.microsoft.com/cpp/build/clang-support-msbuild?view=vs-2019.
If you want to take a look to supported generators:
https://cmake.org/cmake/help/v3.18/manual/cmake-generators.7.html
Solution 3:[3]
In case anyone coming here is new to CMake...
I use CLion and downloaded the Visual Studio command-line tools. The command I run to build my project on Windows is:
cmake -DCMAKE_BUILD_TYPE=Release \
-G"Visual Studio 17 2022" \
-T ClangCL \
-DCMAKE_C_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe" \
-DCMAKE_CXX_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe" \
-S ./build
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | yflelion |
Solution 3 | user1791622 |