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.

Your post is very helpful; I successfully built UE 5.3.0 with Clang using the same settings as you, however the program always hangs on startup at around 90%, when it starts asynchronous asset loading, unless I disable multithreading.

Did you also encounter this issue when building with Clang? If not, could you share what Clang version and Windows SDK version you’re using? Thanks very much.

You are welcome, I am glad you found my post helpful.

Unfortunately, I did not build UE from source, I download the binary build from Epic Games Launcher and use Clang to build my C++ source code, not the engine.

For every UE version you would like to use, you should check the release notes of that version and scroll down to Platform SDK Upgrades section and check what Epic Games recommend for each version. (Ctrl+F in browser, type “farm” to get there quickly)

In your case for Unreal Engine 5.3:

  • Visual Studio 2022 v17.4 or newer
  • Windows SDK 10.0.18362 or newer
  • LLVM clang 14.0.1
  • .NET 4.6.2 Targeting Pack
  • .NET 6.0

IDE Version the Build farm compiles against

  • Visual Studio: Visual Studio 2022 17.4 14.34.31933 toolchain and Windows 10 SDK (10.0.18362.0)

Some people reported that setting <CompilerVersion> in BuildConfiguration.xml to Latest instead of manually typing your version e.g. 14.0.1; is necessary or it might have problems or something I can’t remember (experiment to check it out, I don’t have links or source to prove that). So having 1 LLVM installation might be the way to go. Also make sure it is installed in C:\Program Files\LLVM and it is added to PATH, I guess UE expects LLVM to be at its default directory.

Finally, from my last post, the BuildConfiguration.xml shown was the configuration I had set to build with Clang before I left back for MSVC.

<?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>

Good luck!

1 Like

Ah perfect, I just had the wrong version of Clang. Switching to 14.0.1 fixed it. I saw here that up to Clang 16.0.0 is supported, but that must be in newer versions; I should have checked the version release notes.

Thanks for the help!

You are welcome!