Metric Panda Games

One pixel at a time.

Ways to Compile with Clang on Windows

Rival Fortress Update #27

Clang is my compiler of choice as I found it to be the fastest for development and it works on all desktop platforms.

In this post I’ll talk about how you can setup Clang on Windows without having to cross-compile from Mingw or Cygwin.

Flavors of Clang on Windows

There are currently two flavors of Clang that work on Windows: vanilla LLVM Clang, and Clang/C2 with Microsoft Codegen.

LLVM Clang is, at the time of this writing, mostly feature complete but is not able to generate PDB debugging information. It comes with two executables: clang.exe and clang-cl.exe. The former supports normal Clang-style command-line options, while the latter supports MSVC-style command-line options.

Clang with Microsoft Codegen, uses the Clang frontend and Microsoft’s MSVC backend to generate machine code, so it can output PDB files. It can be installed as part of Visual Studio 2015 and can be uses from the IDE or command-line.

Using Clang with GDB on Windows

In order to make Clang emit DWARF debug symbols that GDB understands, you have to use the LLVM linker so you the full LLVM installation.

The following line will compile and link an executable that can be debugged on GDB:

clang.exe -O0 -gdwarf -c test.cpp -o test.obj && lld-link -debug test.obj

Using Clang with Visual Studio

Using Clang with Visual Studio is straightforward if you use the version that comes bundled width the IDE. To enable it follow the steps described in the official blog post. I personally haven’t tried this, as I don’t use Visual Studio.

Using Clang with CMake and Ninja

My development setup relies on CMake and Ninja, but when it came to setting up Clang on Windows I had to hack around quite a bit to get it to work.

The main problem I had to wrestle with was that CMake kept incorrectly detecting MSVC no matter what flags I set.

The hackish solution I found that works with version 3.6.0 is to use the following command:

cmake -H. -G Ninja -Bbuild -DCMAKE_C_FLAGS=TRUE -DCMAKE_CXX_FLAGS=TRUE -DCMAKE_C_COMPILER="C:/Program Files/LLVM/bin/clang.exe" -DCMAKE_CXX_COMPILER="C:/Program Files/LLVM/bin/clang.exe" -DCMAKE_LINKER="C:/Program Files/LLVM/bin/lld-link.exe"

As you can notice I have to force the C and CXX flags to TRUE. This seems to be the only way stop CMake from detecting MSVC.