r/cpp • • 3h 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!)

12 Upvotes

7 comments sorted by

•

u/13steinj 3h 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?

•

u/0navs 2h 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!

•

u/13steinj 2h ago

The linked doc feels very Bazel/Google coded.

•

u/0navs 2h ago edited 1h 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.

•

u/gracicot 1h 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.

•

u/13steinj 1h 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.

•

u/0navs 3h ago edited 2h ago

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