How-To's Tips & Tricks

Setting up the Clang Compiler in CLion on Windows

MSYS2 now provides the majority of packages built with clang. They use libc++ and lld by default, and you can get the complete toolchain by getting packages from this page.
Be aware that when using libraries built with clang it’s better not to mix them with libraries built with other toolchains. You can find the list of all libraries here.
As a make executable for CLion, you can use clang64/bin/mingw32-make.exe. Don’t forget that with default build flags you will need to have libc++.dll and libunwind.dll in your build directory.
Otherwise, this approach might be even easier to use than the original one explained in this article.

Original article:
With CMake 3.15 it has become possible to use the Clang compiler on Windows with a GNU-style command line. This also means that it’s possible to use Clang with the Mingw-w64 toolchain.

While it’s possible to configure Clang with the Mingw-w64 (or MinGW) toolchain, if you decide to use Clang from the LLVM repo, it won’t work correctly. This is because LLVM Clang for Windows is built using Microsoft Visual Studio, and all the built-in macros and include search paths are set up for use with Visual Studio. So even when used with the MinGW toolchain, it will still try to include MSVC standard library headers.

We’ve done some experiments and found a possible workflow that involves using CLion in combination with the MinGW toolchain and Clang compiler.

Here are our findings:

  1. Install MSYS2 from, follow their installation guide.
  2. Get the necessary packages with the pacman tool. We installed the following (pacman -S):

    • mingw-w64-x86_64-gcc
    • mingw-w64-x86_64-clang
    • mingw-w64-x86_64-lld
    • mingw-w64-x86_64-gdb
    • mingw-w64-x86_64-make
    • mingw-w64-x86_64-polly
    • mingw-w64-x86_64-compiler-rt

This clang compiler is built with mingw-w64 and has paths and macros that correspond to this toolchain.

Now we are ready to set up the CLion toolchain. Go to Settings/Preferences | Build, Execution, Deployment | Toolchains:

Toolchains MinGW with Clang

With your new toolchain configured, you can start building your project. You can use either the default ld linker or set the lld with -DCMAKE_LINKER=lld.

Using advanced tools provided by the Clang compiler

In theory, all Clang tools should also work without any issues. However, problems can arise involving compiler-rt. compiler-rt is a set of runtime libraries which are required to use sanitizers and profiling in Clang, the current compiler_rt package was built with MinGW. But Clang requires a compiler that is built with Clang and lld.

In our case, we wanted to use profile-guided optimization. One of the options for making this work is to get the compiler-rt source code of exactly the same version as the Clang version in MSYS2. This can be challenging, so the other solution is to clone the LLVM monorepo and build the tools you need.

For -fprofile-instr-generate it might be enough to build only compiler-rt and llvm-profdata to merge the profiler results. However, to use all the tools reliably, it’s better to build Clang and lld, as well.

Luckily we already have the required setup for that build.


The last step is to replace the binaries in <msys2_path>\mingw64\lib\clang\<clang_version>\lib\windows with libraries from <compiler-rt_path>\cmake-build-release-mingw_clang\lib\windows or from <llvm_build_path>\lib\clang\<clang_version>\lib\windows in case of monorepo.

Using Clang for profiling

With the compiler-rt libraries in place, it’s now possible to use the same toolchain we set up with -fprofile-instr-generate/-fprofile-instr-use flags. Let’s build LLVM for this experiment, since we already have the source code. We’ll also use -DLLVM_ENABLE_LTO=Thin for even more optimization. Go to Settings/Preferences | Build, Execution, Deployment | CMake:

Clang config

With this CMake configuration, you can build the Clang compiler and use it, for example, to build your own project. This will generate the relevant profiler information, which should later be merged with the tool we built earlier – llvm-profdata. With the merged profile_merged.profdata file, you can finally build the optimized version of the Clang compiler:

Clang profile use config

Using custom Clang and lld

Getting the gcc-style -fprofile-generate/-fprofile-use flags to work correctly requires changing the Clang path and setting -DCMAKE_LINKER to the newly built lld. You also need a few additional tricks for LLVM : -femulated-tls and linking pthread.Clang self-build

Clang gcc profile generate

Then you should repeat all the steps you performed with -fprofile-instr-generate/-fprofile-instr-use.


Using Clang on Windows is now possible and does not require you to have Microsoft Visual Studio installed!

We hope that in the near future, using advanced clang tools will become easier and will no longer require the manual build. Let us know if you discover any other ways to achieve the same results!

Your CLion team
The Drive to Develop

image description