Home

Awesome

Clang on Windows

Building LLVM/Clang

Commands for building LLVM/Clang on Windows.

LLVM/Clang Windows Prerequisites

Instructions

  1. Create a directory to sync the LLVM Project repo to.

    cd ${SRC_ROOT} # root folder for LLVM repo
    git clone --depth 1 https://github.com/llvm/llvm-project .
    
  2. From the root folder, configure the build for LLVM.

    cmake -S llvm/ -B build/ -G "Visual Studio 16 2019"^
        -DCMAKE_GENERATOR_PLATFORM=x64^
        -Thost=x64^
        -DLLVM_INCLUDE_TESTS=OFF^
        -DLLVM_BUILD_TOOLS=ON^
        -DLLVM_INCLUDE_UTILS=OFF^
        -DLLVM_TARGETS_TO_BUILD=X86^
        -DCLANG_ENABLE_STATIC_ANALYZER=OFF^
        -DCLANG_ENABLE_ARCMT=OFF^
        -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld"
    
  3. Build LLVM/Clang.

    cmake --build build/ --target install --config Release
    
    • The --target install command will copy built files (.lib, .exe etc.) to C:\Program Files (x86)\LLVM by default. This location can be customized by setting the -DCMAKE_INSTALL_PREFIX variable when running the configure step.
      • If a custom install location isn't set and a Debug version of the library is built/installed, the files will overwrite the previous Release files (whichever you built last will win). Please see the example files in examples/llvm-build-scripts for an example of how to install both Debug and Release versions using -DCMAKE_INSTALL_PREFIX.
  4. More information can be found here on using LLVM and CMake.

Ninja

It is possible to use the Ninja build system on Windows (even when using the MSVC (cl.exe) compiler) as opposed to MSBuild. This is useful if you want to generate a compile_commands.json file for your project.

Ninja Prerequisites

The exact location of ninja.exe and vcvars64.bat may differ depending on which version of Visual Studio you have installed (year/edition etc.).

Projects

Build with Clang

There are several options available when building your project.

Use Clang Libraries (LibTooling)

In addition to being able to use Clang to compile, with Clang/LLVM installed, it is possible to build projects making using of libTooling. It is possible to use the libTooling libraries with MSVC or Clang.

Please refer to use-llvm-simple/CMakeLists.txt for an example of how to setup a project that uses Clang and LLVM as dependencies and the use-llvm-simple/configure-debug.bat for how to configure the project.

include-what-you-use

With the LLVM and Clang libraries installed it's possible to build include-what-you-use to analyse C and C++ source files.

Please see examples/iwyu-build-scripts for commands to build the library.

Once iwyu is built, add the location of include-what-you-use.exe to your path and then invoke iwyu_tool.py in the directory of a compile_commands.json file of a project you've already built.

# analyse includes, write output to file
python C:\path\to\iwyu\iwyu_tool.py -p . > iwyu.out

# apply fixes
python C:\path\to\iwyu\fix_includes.py < iwyu.out --blank_lines --nocomments --reorder
# optional --dry_run

Please find more information on include-what-you-use here.