r/cpp • u/TheRavagerSw • Feb 26 '26
Will we ever have a backwards compatible binary format for C++ modules?
I'm not talking about modules interface files, those are just headers without macros. What I mean is that I have a cpp source file that exports a module and I compile it and use it a later standard. Or a higher compiler version.
Modules currently, just like most of C++ features are extremely inconsistent, you either lock your toolchain very tightly and use compiler artifacts, or you build everything in your project.
Both approaches aren't really favourable, having something backward compatible and something not compatible is weird. İt is just so inconsistent.
C++ isn't a centralised language, we have multiple stdlibs compilers etc. So expecting everything to be build by a single build system is a harmful fantasy.
Do people on the committee actually care about how we build stuff? Because the way modules work now is so inconsistent it does not make sense.
16
u/Jannik2099 Feb 26 '26
No. Compiled module artifacts are essentially just a serialized frontend AST.
The conversion from source -> AST is inherently compiler specific (duh) and also depends on the chosen standard and other codegen flags (think e.g. -fshort-wchar)
The stable interface for modules is the interface files.
15
u/the_poope Feb 26 '26
I wouldn't count on it.
But it is IMO not a problem. The main benefit of modules is that it potentially speeds up compilation of inline functions and templates. This is nice during development when you iterate and go through the write-run-debug cycle. During development your compiler and toolchain hopefully doesn't change very often. So a standardized + backwards compatible binary module format isn't really that useful.
7
u/tartaruga232 MSVC user, r/cpp_modules Feb 27 '26 edited Feb 27 '26
I think you have some fundamental misunderstandings there.
Compilers compile module interfaces to binary BMI files. BMI means "built module interface". It is a binary representation of the interface that was originally present in C++ syntax.
If you write
import A;The compiler wants to read the BMI file of A to have the reachable declarations of A available. But the BMI of A does not need to have a stable, documented format and is built on the fly by the compiler. Instead of parsing the source of A over and over again while building a program or library, the source file of A needs only be parsed once.
The artifact which is shipped to users of e.g. libraries is the C++ source, not the BMI files. And that's not a problem. BMI files are an internal detail of compilers and are specific to a build (e.g. compiler flags affect the resulting BMI).
So BMI files are sort of a cache of the C++ source of a module interface. That is the file containing
export module A;Modules define an ordering, so compilers need to build the BMI files before they can import them. But BMI files are not used across different builds on different computers. They act like cached results of parts of the build.
Build speedup is just one beneficial aspect of C++ modules. But it indeed manifests itself from my personal experience. The biggest win is using
import std. I've converted the sources for our UML Editor to using modules. Usingimport stdin this project reduces the time to build the whole program from ~2:30 minutes to ~2 minutes. I like that, but this is not the only benefit of modules.2
u/Boring_Intention_336 Mar 01 '26
Since the lack of a standardized binary format basically forces you to rebuild your entire project every time you update your toolchain, you might find that using Incredibuild makes those massive recompiles much less of a chore. It essentially spreads the build load across your network so you aren't stuck waiting for hours on a single machine. This lets you keep your toolchain flexible without being penalized by the current state of C++ modules.
16
u/daveedvdv EDG front end dev, WG21 DG Feb 26 '26
What exactly do you suggest the committee's role should be wrt. this issue?
I believe the Microsoft IFC format (https://github.com/microsoft/ifc) can absorb release-to-release evolution.
(u/GabrielDosReis: Can you confirm?)
Some implementations prefer to serialize their internal representation more directly, which makes release-to-release changes harder to handle, but it has the potential to improve performance.
4
u/green_tory Feb 26 '26
You'd need some sort of common intermediate language that can be retargeted for each platform.
3
u/13steinj Feb 27 '26
No. Modules are a thin poorly conceived "pch++."
I generally feel as if there had been a group of people slowly pushing for various things (build speed, a build system and compiler aware way to share code, ABI compatibility) and the committee shoehorned in a solution that takes poor pieces of each component and gave misleading benchmarks to shut this group of people up.
The subgroup eventually took their ball and went home because (my poor, outsider view is) they felt unheard / that the committee was disinterested in continuing to home this work, and it is not a necessary pary of a language standard.
-1
u/[deleted] Feb 26 '26
[removed] — view removed comment