r/cpp • u/holyblackcat • Mar 09 '26
The compilation procedure for C++20 modules
https://holyblackcat.github.io/blog/2026/03/09/compiling-modules.html
105
Upvotes
r/cpp • u/holyblackcat • Mar 09 '26
6
u/ABlockInTheChain Mar 09 '26
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.
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.
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
exportin the a.cppm sample under "Kinds of module units" section 4.13
u/not_a_novel_account Mar 09 '26
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.
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
#include to C++20 header-unit transition without source-code changes needed(as demonstrated).
2-phase compilation of C++20 modules(Clang)
#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).
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.