Skip to content

Compiling with Clang

John Stewart edited this page May 15, 2022 · 12 revisions

stability-beta

Clang Setup

To compile CommonLibSSE NG with Clang, an LLVM install with clang-cl.exe must be available in the path.

Note: Currently due to an issue in SKSE's lookup of exports from DLLs, it is not possible to load a plugin linked with LLVM's lld-link.exe; therefore while LLVM can be used to compile an SKSE plugin, Visual Studio must be used to link it. Workarounds are currently being investigated.

Via Visual Studio

The simplest way to do this is to install LLVM/Clang through the Visual Studio Installer. In the installer, select Modify on your Visual Studio install. Then go to the "Individual components" tab. In this tab, you should enable the following two items:

  • C++ Clang Compiler for Windows (13.0.0)
  • C++ Clang-cl for v143 build tools (x64/x86)

Note: The specific versions of the Clang compiler and build tools referenced above may change over time.

After completing the install, restart your IDE if it was already running.

Standalone Installation

You can also install LLVM separately. This is the best way to get the most up-to-date version. You can find LLVM releases on their GitHub releases page. Find the latest version and download the LLVM-{version}-win64.exe installer. Be sure to check the option to add LLVM to your PATH environment variable.

After completing the install, restart your IDE if it was already running.

Clang CMake Presets

The CommonLibSSE project provides presets specifically for Clang. To build with Clang in Visual Studio, in your top menu bar, select a configuration preset that includes Clang (the most likely one you want will be Debug-All-ClangCL or Release-All-ClangCL). Select the build of the same name (it will be the only option for the build preset). If you are using Visual Studio Code, these options will be in the bottom status bar. In CLion, you should open your Settings (File->Settings) and then look under Build, Execution, Deployment->CMake to find your CMake profiles. Enable the Debug-All-ClangCL and Release-All-ClangCL (or any other presets you want; you can ignore the -configure presets). You will now build with Clang.

Issues with Clang Compilation

There are several things to keep in mind when building with Clang compared to MSVC:

  • Clang has limited support for consteval. It is possible to enable some consteval dependent features by forcibly defining __cpp_consteval, but it still has some issues with running consteval functions in an immediate context. Of particular note is that this limits the use of std::source_location; CommonLibSSE NG has defined SKSE::stl::source_location that works as an alias for std::source_location on MSVC and has a compatible implementation on Clang which uses constexpr instead of consteval to avoid this issue.
  • A bug in Clang causes it to incorrectly warn on classes that inherit from multiple parent template classes of type BSTEventSink<T>. This requires -Wno-delete-non-abstract-non-virtual-dtor passed to the compiler to ignore. If using CommonLibSSE NG via the Color-Glass vcpkg repository, this will be handled automatically in your project.
  • You may need to add #undef cdecl to avoid an error during CMake configuration. This seems to be needed when using a separately-installed Clang, rather than the version installed via Visual Studio Installer. This fix is included in the PCH.h file in CommonLibSSE NG, but if you include a Windows header after the inclusion of CommonLibSSE NG then you may need this as well.
  • Linkage should be done with Microsoft's link.exe and not LLVM's lld-link.exe. This is due to SKSE not finding the correct DLL exports when an unnamed export is included in the export address table; the LLVM linker always produces an unnamed null entry for ordinal 0.