How to use CMake to setup Visual Studio for using the Clang compiler

This is a brief post on how to use CMake to setup a Visual Studio 2019 solution on Windows that will use the LLVM Clang compiler instead of the orginal Visual C++ one.

I. Prerequisites

This is not a tutorial on the basics of CMake or the usage of Visual Studio, so therefore I’m simply assuming that you already have Visual Studio installed for C++ development on your machine. (If not, you can start by getting the gratis Community edition from Microsoft.)

Similarly, I take it as given that you are already familiar with CMake. If not, plan some time for it: It is very useful, but has a rather steep learning curve after the first steps, mostly due to the bad official introduction material and documentation.

I will just be using the Clang compiler from the LLVM suite of tools — the build and link environment for Windows will still be provided by Visual Studio. That means, we first have to adjust the Visual Studio installation a bit…

II. Prepare the Visual Studio installation for Clang

There are two ways to make use of Clang together with Visual Studio 1:

  1. Either install the version of Clang that Visual Studio itself offers:

    1. Run the Visual Studio Installer and select “Change…” to modify the existing installation
    2. Select “Workload: ‘Desktop development with C++'”
    3. Look in the list of optional components on the right
    4. Select “C++ Clang tools for Windows”
  2. Or download and install LLVM (which includes Clang) separately on your machine and then install a small Visual Studio component:

    1. Get a setup package of pre-built binaries of LLVM for Windows, which are available at https://releases.llvm.org/download.html, e.g. LLVM-11.0.0-win64.exe. (For ease of use later on, I recommend to choose “Add LLVM to the system PATH for all users” during the setup.)
    2. After LLVM is installed, run the Visual Studio Installer and select “Change…” to modify the existing installation
    3. Select the tab “Individual components”
    4. Select “C++ Clang-cl for v142 build tools (x64/x86)”

III. Configure a Visual Studio solution with CMake to use the Clang compiler

(Again, I’m assuming that you already have a project folder ready, with a prepared CMakeLists.txt file and your source code in it…)

At the CMake configuration step, set the Generator (-G) to a value for Visual Studio and the Toolset (-T) for it to ClangCL:

C:\MyProject> cmake -S source_directory -B build_directory -G "Visual Studio 16 2019" -A x64 -T ClangCL

Note that the CMake documentation is (also) pretty much non-existing on examples or references on which values are valid for -T, and the tips on the Internet are often outdated: -T LLVM only works with Visual Studio 2017; for Visual Studio 2019, use -T ClangCL instead! 2

IV. Check: Did it work?

Command Line

The CMake configuration command from above should output something along these lines:

-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The CXX compiler identification is Clang 11.0.0 with MSVC-like command-line
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/LLVM/bin/clang-cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done

Visual Studio GUI

If you open the generated *.sln file with Visual Studio, then the project should be displayed with a fitting suffix, like "<ProjectName> (LLVM - clang-cl)", in the navigation tree.

Additionally, if you open the project’s properties page, you should see this under Configuration Properties ➔ General ➔ Platform Toolset: “LLVM (clang-cl)”.


  1. https://stackoverflow.com/a/38174328
    https://docs.microsoft.com/en-us/cpp/build/clang-support-msbuild ↩︎

  2. https://gitlab.kitware.com/cmake/cmake/-/issues/20415
    https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5756 ↩︎