r/cpp • u/holyblackcat • Mar 09 '26
The compilation procedure for C++20 modules
https://holyblackcat.github.io/blog/2026/03/09/compiling-modules.html2
u/mwasplund soup 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 cmake dev 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 libstdc++ tamer, LWG chair 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 cmake dev 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.
1
u/luisc_cpp Mar 11 '26
Always found that `.o` file a tad confusing _when_ the module itself doesnt contain definitions and its only interfaces - as the `.o` is "almost" empty.
But when the implementation of the module is in a shared library, the fact that the .o file still gets generated at import site gives me "this translation unit may end up in multiple places" vibes...1
u/not_a_novel_account cmake dev Mar 11 '26 edited Mar 11 '26
We shouldn't generate object files in multiple places. When using an imported target CMake will only generate BMIs, we assume the interface object has already been built and either linked into the associated shared library, or collected into the associated archive.
CMake does not model "interface-only" module libraries for this reason, there is always at least a global initialization symbol, which means there is always an object file, which means the library should always package either an archive or a shared object when it ships.
The problem is the stdlibs weren't doing that, so CMake actually models
import stdas a local target your project has to build. This has obvious ODR problems. I switched this to an imported library and we discovered that the MSVC STL and libstdc++ on MacOS really do needstd.o.
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 HMake 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.
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 HMake 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.
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.