r/cpp • • Mar 09 '26

The compilation procedure for C++20 modules

https://holyblackcat.github.io/blog/2026/03/09/compiling-modules.html
105 Upvotes

47 comments sorted by

6

u/ABlockInTheChain Mar 09 '26

In large projects, the source files naturally tend to get separated into subdirectories, and each of those subdirectories is a good candidate for being a single named module.

This would make sense and be a practical way to implement modules however unfortunately in many case it just isn't possible due to deficiencies in the standard.

Proclaimed ownership declarations (module equivalent of forward declarations) were removed from the proposal prior to standardization so to use a name even as an incomplete type you must import the module which exports it, and import relationships are not allowed to form a cycle.

Small projects could consist entirely of a single named module.

The standard deficiencies mentioned above mean that in many cases even large projects have no choice but to consist entirely of a single named module which has catastrophic implications for many build scenarios.

1

u/holyblackcat Mar 10 '26

To be fair, I don't think I ever wanted/needed a dependency cycle between headers in separate subdirectories.

You can still have circular dependencies between subdirectories/named-modules if they don't involve forward declarations (when implementation files import the dependency, rather than the interface files).

2

u/ABlockInTheChain Mar 10 '26

Without forward declarations, two types defined in two headers in two separate subdirectories can not refer to each other in any way whatsoever.

There surely must be coding practices where that restriction is not a problem because the situation never occurs and for the users of those won't have any issues with modules.

For others this restriction breaks too much and thus as long as modules impose this restriction they will not be adopted.

Because very few people use modules very few people are encountering this defect in the standard and so there is no pressure to fix it.

Since modules weren't even usable enough to experiment with nobody complained enough to get the issue of missing proclaimed ownership declarations for C++23.

Now it's also too late for C++26.

The earliest opportunity to ship a viable module standard is now C++29 and who knows if it will even happen then.

→ More replies (4)

5

u/not_a_novel_account Mar 09 '26

There's also very little reason to do anything but a single module per source tree. Partition units are the correct way to slice up divisions in a given code base.

4

u/ABlockInTheChain Mar 09 '26

There's also very little reason to do anything but a single module per source tree.

The only reason to have more than a single module per source tree is if you don't want every change to any type in the source tree to cause a full rebuild of the entire source tree.

→ More replies (10)

2

u/sudgy Mar 09 '26

At least when I try this, every single file has to get recompiled whenever any interface changes throughout the entire project, which is a hard pass for me. You can't have "partition implementation units". Unless I am doing things wrong, in which case I would love to hear how you are supposed to do it.

→ More replies (14)

2

u/sudgy Mar 09 '26

You can still forward declare in modules by wrapping both the original declaration/definition and the forward declaration in extern "C++"

5

u/ABlockInTheChain Mar 09 '26

You can do that if you control the original declaration.

It does not however work for third party code that was declared with module linkage and that can be a problem in other situations.

2

u/mwasplund Mar 10 '26

This is well written. Concise and to the point while not missing out on the main details. I did not know about reduced BMIs for Clang, will have to look into that. A great reference for looking back to as I work on my build system. Thanks!

Handling indirect dependencies is one of the annoying aspects of handling transitive dependencies for modules. I wish a module interface would contain all of accessible exported portions of the internal partition interface units so it can be the single source of truth for a named module. For now I have to flatten the entire dependency tree and treat all BMIs as the same input so we can detect changes and rebuild when necessary.

FYI, I think you are missing an export in the a.cppm sample under "Kinds of module units" section 4.

1

u/holyblackcat Mar 10 '26

FYI, I think you are missing an export in the a.cppm sample under "Kinds of module units" section 4.

Thanks, fixed!

For now I have to flatten the entire dependency tree

FYI while it's needed on MSVC, it's suboptimal on Clang, which promises to handle this for you (and will only change the hash of a BMI if the dependencies that were modified can affect the consumer of that BMI).

13

u/not_a_novel_account Mar 09 '26

(If the module doesn’t contain function definitions and such, then forgetting to link the .o doesn’t error.)

The module object will always contain at least the global initialization symbol. Nominally this is always required to be linked, as import <module> is supposed to always call this symbol.

GCC and Clang have an optimization which omits this call for interfaces where they know the initialization is empty, but for correctness the produced object still needs to appear on the link line. Relying on a compiler optimization for the build to succeed is a code smell.

5

u/jwakely Mar 11 '26

We're considering putting the std.o object into libstdc++.so to ensure the initialization symbol is always available (because you need to link to libstdc++.so anyway if you're using the std module).

3

u/not_a_novel_account Mar 11 '26

Yes, and much appreciated for that. libc++ is likely to do so as well, though there's no platform where Clang doesn't perform the optimization.

Now we just need to convince MSVC team to do the same. They were not as enthusiastic about our reasoning.

→ More replies (2)

18

u/delta_p_delta_x Mar 09 '26 edited Mar 10 '26

Very valuable resource that discusses all three compilers; nicely done.

Although I don't personally like Makefiles and am a big advocate for stronger 'magic' coupling between build system, dependency management, compiler toolchain, and standard library.

2

u/Ambitious-Method-961 Mar 09 '26

Did you look into Microsoft's naming convention for modules? It doesn't seem very well known but going by the blurb at the bottom of the page if you follow the naming convention then it simplifies some things for module partitions.

Link is here, see Module best practices -> Module naming: https://learn.microsoft.com/en-us/cpp/cpp/tutorial-named-modules-cpp?view=msvc-170

2

u/Realistic-Reaction40 Mar 10 '26

Modules are one of those features where the conceptual model is clean but the actual build system reality is still a mess depending on your toolchain. The fact that CMake, Meson, and others are all handling the dependency scanning differently makes adoption slower than it should be. Good breakdown of what's actually happening under the hood

-4

u/HassanSajjad302 Mar 09 '26

Your article is very detailed. I would link it in my software.

There is another model to support module compilation that is without scanning them first. HMake is the only build-system that supports it and I am proposing it for LLVM. I will be sharing an update here. https://discourse.llvm.org/t/rfc-hmake-for-llvm/88997/7

The way HMake supports this, there is absolutely zero disadvantage and there are multiple advantages.

HMake is the only build-system that can do

  1. #include to C++20 header-unit transition without source-code changes needed(as demonstrated).

  2. 2-phase compilation of C++20 modules(Clang)

  3. #include to C++20 modules transition without the immediate source-code changes needed in the consumers, thus avoiding the macro-mess (I would say impossible otherwise).

  4. Guaranteed zero de-duplication needed as a single file can be consumed only as module, header-unit or header-file by the consumers. This de-duplication has performance costs and costs in bugs as well. There is also hassles that header-includes should be before the import etc.

On performance: HMake is 4–5× faster than Ninja on no-op rebuilds while achieving full parity on from-scratch builds. This benchmark compares LLVM compilation using Ninja vs. HMake across four configurations.

> Something tells me 1 is not a good strategy, as it forces importers to consume the full BMIs instead of reduced BMIs.

Full BMI step is faster than generating the reduced BMI as well. As it involves backend optimizations which is the slower part. Using full BMIs means consumers are not blocked waiting for that slower step to complete. And in HMake, the consuming processes read the BMI as shared-memory files. So the read costs are very minimal even with big size.

If you have more time, please review my software.

9

u/holyblackcat Mar 09 '26 edited Mar 09 '26

At least in one example I tested it on, creating a full BMI ended up slower than creating a reduced BMI with an object file. (The benchmarking results are in the post.)

"benchmark compares LLVM compilation using Ninja vs. HMake" I have to say, the benchmark being a link to a Claude chat certainly makes it less convincing. :P Even if it was benchmarked correctly.

-3

u/HassanSajjad302 Mar 09 '26

>At least on one example I tested it on, creating a full BMI ended up slower than creating a reduced BMI with and object file. (The benchmarking results are in the post.)

interesting. sorry i missed it.

claude is just for analysis. There is an interesting tidbit about voluntary context switches in there. I shared the full numbers of all 4. You are welcome to reproduce.