C++20 Modules
I started digging into C++ 20 Modules. If you're not familar, here is a good introduction. Compared to other languages like Rust, I found official references and examples sparse or incomplete. Microsoft has a short intro. I found these Vector of Bool posts insightful, although the writer has seemed to receive C++ Modules with a high degree of criticism. I find the Clang docs unclear about a number of important things. Things are slightly different with GCC.
First of all, with Clang 12, you need to opt-in to modules with -fmodules
. I'm not sure why
-Xclang -emit-module-interface
is a front-end option still. It seems obscure,
you can only find it from running clang -cc1 --help
. But you need it because you're expected to
precompile modules (I made a gist that demonstrates the work flow). Then when you build the
file that imports the module, you need to specify where the prebuilt module is located.
It's awkward that the compiler wants to know the module names. Your compiled module's file name
must match the module name or you can provide a special flag that maps the module name to an arbitrary file
(ie -fmodule-file=name=./file.pcm
).
C++ Modules are literally an after-thought, and they have the DX of one. They get a lot more complex than this, I will spare you the rather painful details. Honestly, I'm not sure if they're going to appeal to anyone who's got a large C++ codebase. But for new projects they seem to be an improvement.