Prior to the introduction of modules, the compilation process for a C++ program went something like this:
- Each
.cpp
file corresponds to a translation unit. The compiler compiles each.cpp
file into an object code file. - Those object files are then linked together.
- If a
.cpp
file#include
s some other file, the the contents of that file run through the preprocessor, and the result is copied and pasted into the destination.cpp
file.
How does this change with the introduction of C++ modules? Are we to understand each source file as being a translation unit in the same way, or has the conceptual understanding of this changed somewhat?
Prior to the introduction of modules, the compilation process for a C++ program went something like this:
- Each
.cpp
file corresponds to a translation unit. The compiler compiles each.cpp
file into an object code file. - Those object files are then linked together.
- If a
.cpp
file#include
s some other file, the the contents of that file run through the preprocessor, and the result is copied and pasted into the destination.cpp
file.
How does this change with the introduction of C++ modules? Are we to understand each source file as being a translation unit in the same way, or has the conceptual understanding of this changed somewhat?
Share Improve this question asked Feb 16 at 9:59 user2138149user2138149 17.2k30 gold badges145 silver badges287 bronze badges 4 |1 Answer
Reset to default 3A module may consist of a single file or a multitude. Each file is a translation unit compiled to binary.
Thinking of a module as a funny header is as misleading as considering a reference a funny pointer. Modules and references both address similar needs to their counterparts but in a different, hopefully better, manner.
The Module Interface Unit creates a Binary Module Interface, which contains the information about the module, including which routines are exported. Besides the interface, the unit contains build system information to ensure the calling system is compatible.
A Module Implementation Unit supplies the exported routines and may use other C++ translation units and libraries.
Note, again, these are all binaries that are linked with the calling code.
A module is independent of the calling code. It is recompiled only when its implementation changes. If the interface doesn't change, the code using it doesn't need to be recompiled; only linking is necessary.
If the Module Interface Unit changes, it is recompiled along with the code using it.
module
does in C++ is perform aninclude
, but rather than performing a simple copy and paste of source code (after running it through the preprocessor) a module instead performs a copy and paste of an abstract syntax tree. Is this description anything like accurate? – user2138149 Commented Feb 16 at 12:03#include
ing a header can affect how an#include
d header is processed [i.e. change the meaning of#include
d declarations], but will not affect a module). – Peter Commented Feb 16 at 22:21C++ Modules Bill Hoffman
- he is author ofcmake
and he spend lots of time to make modules work with various compilers (work still in progress), he is also a good speaker, so it is hard to find better source. – Marek R Commented Feb 18 at 19:52