How to use Clang on Windows ?

Hello everyone !

I was wondering if someone know how to setup Clang on Windows for compiling instead of MSVC.
I’m using the “Build From Source” Unreal Engine 5.3 version (git) and tried several things to make this work but no one works for me.

I installed Clang using both Chocolatey package and Visual Studio Installer.

Things I tried tried:

  • Adding<WindowsPlatform> <!-- Build using Clang instead of Visual Studio 2017 --> <Compiler>Clang</Compiler> </WindowsPlatform> inside UnrealBuildTool/BuildConfiguration.xml. Throwing an error like warning: The element 'BuildConfiguration' in namespace 'https://www.unrealengine.com/BuildConfiguration' has invalid child element 'Compiler' in namespace 'https://www.unrealengine.com/BuildConfiguration'.

  • Forcing using Clang compiler inside my UnrealProject.Target.cs (TargetRules) with this line // Force Clang compiler WindowsPlatform.Compiler = WindowsCompiler.Clang;
    Not working as well and throwing an error : Unable to find valid 14.39.33519 C++ toolchain for Clang x64

It’s an common issue on Windows version of Unreal Engine ? This seems to work on MacOS and Linux so why not Windows ?

Thanks for everyone who read !

I am on the same boat, but I am using binary build instead of source code.

I have successfully made Clang work on Windows, using Clang as the compiler and MSVC as the toolchain. I did not try to use MinGW GCC toolchain because Epic Games did not build against it on Windows, I don’t know if it is recommended.

This is my BuildConfiguration.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration xmlns="https://www.unrealengine.com/BuildConfiguration">
    <WindowsPlatform>
        <Compiler>Clang</Compiler>
        <CompilerVersion>Latest</CompilerVersion>
        <Toolchain>VisualStudio2022</Toolchain>
        <ToolchainVersion>14.34.31933</ToolchainVersion>
    </WindowsPlatform>
    <ProjectFileGenerator>
        <Format>VisualStudioCode</Format>
    </ProjectFileGenerator>
</Configuration>

The last part <ProjectFileGenerator> is not necessary, it just makes UnrealBuildTool run -Mode=VSCode when you click “Generate Visual Studio project files” from the context menu on your .uproject in Explorer.

There are 3 XML files in 3 different directories (each serve a purpose) to choose from to setup Clang as the compiler in BuildConfiguration.xml:

  • %appdata%\Unreal Engine\UnrealBuildTool\
  • YourProjectDirectory\Saved\UnrealBuildTool\
  • %username%\Documents\Unreal Engine\Saved\UnrealBuildTool\ (This one is unique to source builds, it does not exist for binary UE – also not sure if the directory is correct)

Documentation: Use Clang to Build Microsoft Platforms in Unreal Engine | Unreal Engine 5.4 Documentation | Epic Developer Community

After that, you need to run -Mode=GenerateClangDatabase in CMD using UnrealBuildTool.exe to make clangd work.
Directory: C:\Program Files\Epic Games\UE_5.3\Engine\Binaries\DotNET\UnrealBuildTool

clangd: https://clangd.llvm.org/
clangd installation: Getting started

A thread on UE forums of many users trying to make clangd work: Get compile_commands.json

@dzemzmcdonalda found out that -Mode=GenerateClangDatabase is not incremental: UBT GenerateClangDatabase is NOT incremental and recreates whole compile_commands.json each time

Assuming you want to use VS Code with clangd: https://www.youtube.com/watch?v=JxfNn5DYG_o

At this point, it just takes too much time to setup and maintain. I just reverted back to MSVC. I wish I can continue down with LLVM road, but it is not intuitive.

Let me know what happens, I am still interested in it.

If only Epic Games can make a complete guide on how to correctly setup and use Clang on all platforms, since I believe they use Clang themselves. And if it is possible to use Clang on Windows without MSVC toolchain.