r/cpp • • Jul 20 '26

C++ 20 named modules with clang-cl is finally added in CMake 4.4

I've been using named modules in my hobby C++ projects for years but they always trigger some unexpected ICEs when I used MSVC, and I got really tired of experimenting workarounds. After I switched to clang-cl recently, all the related problems I've encountered so far were resolved. The only thing missing now is import std

66 Upvotes

41 comments sorted by

8

u/Air10n Jul 20 '26

MSVC updates on named module are slow and progress is opaque, there are also regressions failing to compile code that was completely fine before. I'm glad I can finally replace it with the open sourced Clang and find some peace

4

u/slithering3897 Jul 20 '26

Slow? They always update it. It's Intellisense progress that's slow and opaque.

7

u/Air10n Jul 20 '26

I mean it's been 6 years, and MS never made a stable version. MSVC team claimed some were fixed but same issues reappear later. I use Resharper to assist IntelliSense, it's not perfect either but it helps.

1

u/starfreakclone MSVC FE Dev Jul 25 '26

I disagree. We have a special section in our rolling patch notes just for C++ Modules: https://devblogs.microsoft.com/cppblog/msvc-build-tools-preview-updates-july-2026/

16

u/delta_p_delta_x Jul 20 '26

The only thing missing now is import std

This is also fixed. You can get this with a nightly CMake build. You'll still need the magic UUIDs (see my comment here), but it otherwise works flawlessly. I use import std; and named modules, and the MSVC standard library. Pretty cool.

Now if only I could convince CMake/VS/Ninja to use the preview 14.52 toolset that I've installed, instead of 14.51...

3

u/playmer Jul 20 '26

Can you not select it via -T for toolset selection? I don’t know offhand what key you’d need to use, but I’d expect that to work. I was able to use it for the clang toolset years ago, and I feel like it’s what you used for the XP toolset as well.

1

u/not_a_novel_account cmake dev Jul 20 '26 edited Jul 20 '26

Toolsets means you're using the Visual Studio generators instead of Ninja, which have a whole bunch of other module shortcomings.

They don't support BMI compatibility, imported module targets, or exporting libraries which use modules.

You can basically do a local build using your project-local C++ modules using the VS generators.

3

u/playmer Jul 20 '26

Ah you’re right, I even was looking at the VS Generator docs, dunno why it slipped my mind they mentioned ninja. I’m away from my PC and it’s been quite awhile since I dabbled in this sort of thing, I wonder if you can adjust the vcvars options to adjust which toolset it makes default and then launch/use that to generate your ninja build?

3

u/delta_p_delta_x Jul 20 '26

I wonder if you can adjust the vcvars options to adjust which toolset it makes default and then launch/use that to generate your ninja build?

vcvars has an argument for that which I've set in my PowerShell $PROFILE, so CLion and VS Code pick it up, but not VS 2026.

2

u/delta_p_delta_x 15d ago edited 15d ago

It turns out that setting the toolset object as documented here tells VS to use the preview toolset even when using the Ninja generator. I use:

"toolset": {
  "value": "version=preview",
  "strategy": "external"
},

which works perfectly.

/u/playmer, CCing you.

1

u/not_a_novel_account cmake dev 15d ago

So it's important to differentiate what's happening here. The toolset object has no impact on the Ninja generator, and setting strategy to external tells CMake the toolset should have no impact on any generator whatsoever.

What it does is record information for the IDE. You're very indirectly telling Visual Studio to launch a shell which has the appropriate environment loaded. This has no impact on shells launched outside Visual Studio, or any development environment which isn't Visual Studio.

1

u/delta_p_delta_x 15d ago

Indeed; I concluded as much from the documentation which echoes what you've said when strategy is set to external. My understanding is that Visual Studio's CMake handling (which are a couple of DLLs in its install tree) uses objects in the preset including toolset and vendor { microsoft.com/VisualStudioSettings/CMake/1.0 { intelliSenseMode ... to configure itself during a CMake configure. This much was evident when, as mentioned above, the toolset was different when configuring with VS Code, CLion, or in a vcvared terminal compared to VS.

Now the last thing left is the debugger configuration, which VS keeps in launch.vs.json... I long for the day when we get a unified thing that allows us to specify everything in code: dependency fetch, configure, build, debug, install, and test scenarios.

3

u/Kridenberg Jul 20 '26

And again. IMHO, Clang-Cl biggest (I would say even the only real, but again, there may have specific cases) purpose is to be compatible with Visual Studio. Clang-Cl still cannot be used to compile modules in VS, thus there is nothing big to celebrate, TBH. And as far as Clang-Cl modules issue was already off-loaded by vendors to build systems, I guess we will never see Clang-Cl with modules in Visual Studio (they close those requests)

1

u/Air10n Jul 20 '26

It's fine for some cross platform projects that use CMake build system. As for VS only projects header is still a better option, module + template always easily break MSVC

4

u/Kridenberg Jul 20 '26

I moved proprietary game engine from header to modules in MSVC, and also uses modules only for my own pet project (with CMake/clang-gnu cross-compile) for almost 3.5 years. MSVC is just a pure shit. The only good stuff was only the fact that they were first (IntelliSense is pure garbage with modules RN)

1

u/Air10n Jul 20 '26

Use Resharper along with IntelliSense, it helps a bit

1

u/Kridenberg Jul 20 '26

Yeah, I paid for it, but again, VS is also a paid product to some extent

1

u/Air10n Jul 20 '26

IntelliSense is also terrible in VS Code, I wish clangd add proper support for module someday

1

u/Kridenberg Jul 20 '26

I was able to configure ClangD with modules in Zed. First indexing is awfully long, but works in general

1

u/ReDr4gon5 Jul 20 '26

Did moving to modules improve build times? And we're you using PCH files or unity/jumbo builds before using modules?

3

u/Kridenberg Jul 20 '26
  1. Migration to modules itself did nothing regarding the build time, BUT... Build time was improved mostly to the "archiutecture" points that were made during the migration (ability to get rid of some hacks in our code base, reduced PIMPL, e.t.c). Incremental build on the other hand - huge improvement, nearly x3\x4.
  2. We used PCH, but not unity build.

7

u/gracicot Jul 20 '26

On another note, ninja just fixed a bug with modules so flipping dependencies should be handled gracefully: https://github.com/ninja-build/ninja/issues/2666

CMake + ninja are quite well positioned for named modules.

3

u/cristianadam Jul 20 '26

If you want a faster clang-cl have a look at Qt Creator 20: faster clangd

Do not that I haven't tested regular clang-cl there, only against msvc. But the PGO build of clang-cl should be faster than regular clang-cl.

3

u/ReDr4gon5 Jul 20 '26 edited Jul 20 '26

Clang-cl shipped with VS is just a repackaged release from llvm. These are built with PGO already enabled as well as rpmalloc. See the build_llvm_release.bat utility script used. LTO is not used because of the large build time penalty combined with the low gain that testing showed. Maybe that should be retested, PRs upstream are always welcome. Maybe the training workload isn't optimal.

1

u/cristianadam Jul 20 '26

Maybe I should have tested the regular clang-cl 😅

llvm-project/llvm/utils/release/build_llvm_release.bat at main · llvm/llvm-project is the script mentioned. Useful for anybody building their own clang-cl!

10

u/[deleted] Jul 20 '26

[removed] — view removed comment

4

u/Air10n Jul 20 '26

I've been building a rendering engine with C++ modules since 2022, and I really like modules because it's so much cleaner and more elegant than headers, but those compiler bugs make the journey so painful and slow

1

u/_Noreturn Jul 20 '26

Foe my toy project clang-cl compiled faster than msvc. I don't have a clear explanation for why

1

u/Air10n Jul 21 '26

I built NvRTX fork of Unreal Engine 5.7 from scratch using clang-cl and I found the time is similar

1

u/[deleted] Jul 20 '26

[removed] — view removed comment

1

u/_Noreturn Jul 20 '26

My code uses alot of templates my only guess is that clang is better in constexpr evaluation than msvc I did do some tests and found thst clang is faster at constexpr than msvc

3

u/ReDr4gon5 Jul 20 '26

Clang-cl should be faster than msvc. There is a bunch of blog posts on the llvm project blog on how to get there. Also lld-link is way faster than link so use that as well. Some more flags are needed to get optimum performance too.

2

u/kamrann_ Jul 20 '26

My experience is also that clang's compile time performance with modules is worse than MSVC's, especially with heavy GMF includes. But as mentioned above, it does have the advantage of generally not being broken.

1

u/_Noreturn Jul 21 '26

Which is funny since msvc does way less work than clang-cl (e.g it doesn't parse templates until instnastions so invalid syntax compiles) and msvc has terrible optimizations

1

u/starfreakclone MSVC FE Dev Jul 25 '26

This doesn't match my experience. I have put a lot of optimizations into the MSVC implementation to handle many different common patterns we've seen in the wild.

I know awhile back, I had a conversation with a few clang folks discussing what types of optimizations are important to include in a modules implementation. At the time (about 2 years ago now) clang still had a major throughput problem with the GMF because the compiler had to materialize all source ranges up front, which could actually exhaust the pool of possible source locations if you have large GMFs across multiple modules. 

clang has done a great job of catching up, as has gcc. MSVC certainly has bugs, but most things tend to just work now.

1

u/ReDr4gon5 Jul 25 '26

I've never tried modules so can't comment on that.

1

u/Wild_Meeting1428 Jul 20 '26

really, import std is still missing in 4.4, thought that they fixed exactly this?

If its really missing, you can create an own target (add_library) with the stl in it and link it to your target.

4

u/not_a_novel_account cmake dev Jul 20 '26

MSVC doesn't support the normal ABI model for shared libraries when using C++ modules. See CMake issue #27944, Shared libraries with inline definition fail to link under MSVC.

The standard library is a normally linked as a shared library.

This conundrum has held up import std for several releases now. We've settled on doing an MSVC + MS STL specific workaround, but that means reworking several things and I haven't found the time.