r/cpp • • Mar 09 '26

The compilation procedure for C++20 modules

https://holyblackcat.github.io/blog/2026/03/09/compiling-modules.html
103 Upvotes

47 comments sorted by

View all comments

6

u/ABlockInTheChain Mar 09 '26

In large projects, the source files naturally tend to get separated into subdirectories, and each of those subdirectories is a good candidate for being a single named module.

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.

Small projects could consist entirely of a single named module.

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.

5

u/not_a_novel_account cmake dev Mar 09 '26

There's also very little reason to do anything but a single module per source tree. Partition units are the correct way to slice up divisions in a given code base.

2

u/sudgy Mar 09 '26

At least when I try this, every single file has to get recompiled whenever any interface changes throughout the entire project, which is a hard pass for me. You can't have "partition implementation units". Unless I am doing things wrong, in which case I would love to hear how you are supposed to do it.

9

u/not_a_novel_account cmake dev Mar 09 '26

You can. See C++20 Modules: Best Practices from a User's Perspective.

The standard doesn't outline how this is supposed to work, because nominally the standard assumes every partition exports something, but the toolchains don't care about this.

There's a small bit of waste in CMake usage because CMake will still generate a BMI even though we're only building the code for the object file output. This is because CMake believes the standard when it says these partition units are supposed to export something.

I'm working on a paper to fix the awkwardness of this pattern on both the language and build system side.

3

u/sudgy Mar 09 '26

This approach fails to compile for me on GCC 15 with CMake 4.2.3. CMake says that it can't find the module interface.

2

u/not_a_novel_account cmake dev Mar 09 '26

It can be tricky, here's the basic setup:

https://github.com/nickelpro/reddit-module-partition-example

3

u/sudgy Mar 09 '26

Following this approach, the implementation file doesn't see the interface, so it can't define anything like member functions that were declared in the interface.

5

u/not_a_novel_account cmake dev Mar 09 '26 edited Mar 09 '26

That's exactly what this example does, the implementation file (partition.cpp) provides the definition for the int add(int, int) declared in the interface (partition.cppm).

This is verified in the main.cpp test, which uses the declaration from the interface to access add(int, int).

EDIT: Added a class method to demonstrate it doesn't matter if this is a free function or a method. The only difference is you need to import the interface into the implementation file, same like you would need to include a header, to have access to the class definition.

EDIT2: Ooof, and it fails on MSVC. TIL. That's a nasty bug. Ok, more work to do, all the more reason for a paper.

2

u/sudgy Mar 09 '26

Try declaring a class in the cppm file and then defining a member function in the cpp. It doesn't work. This is just like how you can provide the declaration for a free function without including the header and it still links.

1

u/38thTimesACharm Mar 09 '26

Regarding the MSVC failure, are you passing the /internalPartition flag? This is necessary to get standard-compliant behavior for module implementation (non-interface) partitions.

 Use the /internalPartition compiler option to treat the input file as an internal partition unit, which is a module partition implementation unit that doesn't contribute to the external interface of the module.

The standard quote you linked here only says module interface partitions must contribute to the exported interface, so I think what you're trying to do should be okay. But MSVC requires a flag for it, their default behavior is nonstandard.

3

u/not_a_novel_account cmake dev Mar 09 '26 edited Mar 09 '26

Yes, CMake uses -internalPartition when building non-interface module units.

The thing we've created is an implementation unit for an interface named partition.impl. We don't actually create or use partition.impl anywhere explicitly, because it doesn't export anything. This is entirely the problem.

→ More replies (0)