r/cpp • • 2d ago

Clang -fmodules-driver: C++20 modules and import std without an external build system

Earlier this year, Clang added basic support for building simple programs that use Clang modules or C++20 modules directly from the Clang driver, without an external build system. This includes using the std/std.compat modules: https://godbolt.org/z/sahYMsxna

The documentation landed yesterday (although the feature is already in Clang 23), so I thought I would advertise it here now: https://clang.llvm.org/docs/StandardCPlusPlusModules.html#compiling-modules-using-the-clang-modules-driver

This is still a very experimental feature, and it doesn't support caching yet. Currently, all modules used are recompiled on each invocation, which is especially painful with the large C++ Standard library modules.

The main purpose of this isn't to compete with dedicated build systems such as CMake, but to make modules more accessible. I hope this makes it easier to use modules in smaller examples, and maybe even in some people's first Hello World programs.

(This was part of a GSoC project I did in 2025. Happy to answer any questions!)

36 Upvotes

19 comments sorted by

11

u/13steinj 2d ago

This is still a very experimental feature, and it doesn't support caching yet

... am I drunk? Is this not... nearly the entire point of modules?

9

u/0navs 2d ago

You're right, it is. Caching just isn't supported yet, but we want to add it. It was one of this year's GSoC projects, but it fell through. I will probably only be able to work on it again in spring of next year, but if anyone is interested in working on this, feel free to reach out. Using https://llvm.org/docs/ContentAddressableStorage.html for this would be very cool!

-3

u/13steinj 2d ago

The linked doc feels very Bazel/Google coded.

3

u/0navs 2d ago edited 2d ago

I think all LLVM-CAS work is Apple. It can already be used for caching Clang modules, but for C++ modules, there is a lot of work to be done.

5

u/gracicot 2d ago

Not necessarily. If it reuses the same BMI within the same build invocation, that's enough to get the performance gains of modules. You're still not re-parsing the same header for one TU. As it puts the BMI in a temporary file, I'd assume you still get pretty good gains for a complete rebuild. Just not for incremental ones.

1

u/13steinj 2d ago
  1. Is that not considered "caching?"
  2. In general I am yet to find a case where modules actually improve build times in a way that is statistically significant that does not have an isomorphic mapping to splitting up your TUs in the right places, both from the perspective of build performance and from the perspective of resulting runtime performance of the compiler, outside of cases where the entire project already clean builds in a max of a few minutes (which I don't care about). The few cases that pique my interest, there is no statistically significant performance increase compared to using precompiled headers, at which point I don't understand why modules were standardized since modules are equally if not more fragile and vendor constrained (definitely don't work across compilers!)

To this day I consider the lack of implementation/field experience on modules as massive black mark on the standardization process. Comparatively, it would have likely been less work and a better outcome to enforce (entirely implementation defined or unspecified) precompiled headers. But I'm sure someone will respond that that is not possible because something something outside the realm of the abstract machine something something therefore not meant to be part of the standard.

4

u/fortsnek274 1d ago

The build performance is trivial to see. If you make one of your TUs use import std (or spdlog), it will likely compile faster. Therefore, your entire project will compile faster. As long as the module is reused.

But if you modularise all headers, you'll probably slow down the build. Adding modules has overhead. So you should modularise only the core stuff. std, external libs, your common code.

And yes, it probably won't be much faster than precompiled headers, because, modules are precompiled headers in a sense.

The advantage is that you don't have the restrictions of PCHs, but the disadvantage is that we require all these tooling updates... the reason why I want Intellisense to support modules properly is because I get build performance improvements and it'll stop Intellisense reparsing headers so much.

Comparatively, it would have likely been less work and a better outcome to enforce (entirely implementation defined or unspecified) precompiled headers

That's clang header maps, afaik. It would probably be much easier for implementers. But considering modules in a vacuum, I like export ergonomics. And it forces (one day) your language service to support modules properly rather than sticking with the include system.

But the standard could be improved. Import by file path might be faster. Would be nice to have packages/submodules to control name attachment. Proclaimed ownership in forward declarations. Implementation units unnecessarily import the whole module.

And how do you use modules from external libs in your IDE. Should probably lookup in include paths. And build systems/language services should probably be compiling these things based on compile flags and keeping them in a cache. And if that happens, there should be a way to avoid recompiling a module just because some unused preprocessor defs changed (which I think clang header maps do somewhere). And MSBuild still needs to handle importing dllexports properly.

And if MS were to one day fix the top Intellisense bugs and declare better modules support... I'd still worry that errors will be viral through importers. Like, you import some external lib and what if Intellisense doesn't like some complex template or something... the module fails to parse and all importers up to main fail to parse. But whether a clang modules system changes that, who knows.

3

u/GabrielDosReis 1d ago

The build performance is trivial to see.

And it has been consistently documented numerous times by different people working on different projects.

1

u/fortsnek274 1d ago

I wonder though how it compares with module maps. Maybe there's less overhead for the build system for project and single file compiles.

3

u/GabrielDosReis 1d ago

I wonder though how it compares with module maps. Maybe there's less overhead for the build system for project and single file compiles.

That is about 2 or 3 questions in 1 :-)

Strictly speaking, "module maps" are really more of build system orchestration thing than language thing. The language support for it is "header units"; see the report from the Microsoft Office people on that front when they deployed it in production. You use /ifcMap (MSVC compiler switch) to direct the compiler to pick up a given IFC file corresponding to a given header unit (roughly corresponding to Clang's PCM file).

Third, the standard "header unit" facility is the formalized form (by Richard Smith) of "Clang Modules".

Header units demand ORD checks which named modules generally guarantee by construction so ODR checks are not needed (there are exceptions, but from outside the boundaries of a named module, that is what you get). That means that in general named modules outperform header units.

1

u/fortsnek274 1d ago

Header units. Of course. I'll need to test that.

1

u/fortsnek274 22h ago

Header units, not so practical right now with VS. If you do the /translateIncludes thing to use header units for the std lib, then the compiler and intellisense will compile header units for all std and internal headers, for each project. A huge overhead and stresses your system with all those background processes.

Would really benefit from a solution-wide cache.

3

u/GabrielDosReis 20h ago

Would really benefit from a solution-wide cache.

Please share the feedback with them through the DevCom portal.

1

u/fortsnek274 4h ago

Actually, header units are reused, but only through referenced projects. So not so bad, but along with std and external libraries, a solution-wide cache should still be used.

I'll see if I can suggest it, but it's a Big Ask and requires Design Work and maybe some community feedback over how modules are intended to be used. I can't be totally sure myself.

→ More replies

1

u/kamrann_ 9h ago

I'd like this to be true, but I'm yet to see anything more than anecdotal evidence that it's possible to refactor to modules in a way that speeds up builds by some metric. That's a long, long way from being able to say convincingly that migrating an arbitrary codebase that already has good header health to modules will improve its build times.

Sure if you don't use precompiled headers then switching to imports for std and third party libs is a no brainer. But beyond that, yet to be convinced. 

-3

u/pjmlp 1d ago

In theory clang header maps and Visual Studio 2017 experimental modules, were the existing implementations to standardise.

In practice, C++20 as standardised are quite unlike them, and Intelisense still doesn't quite work two Visual Studio releases later, and not at all on the VSCode C++ extension, radio silence on the announcement that was supposed to take place end of January.

Additionally the Apple improvements regarding build times (explicit modules), and Swift/C++ interoperability, are based on clang header maps, with C++ modules papers not even being listed on the XCode support status.

2

u/0navs 2d ago edited 2d ago

(The godbolt example just showcases the `import std` support since it is more difficult to showcase examples with multiple files on godbolt)