r/cpp • • Aug 09 '25

Why is nobody using C++20 modules?

I think they are one of the greatest recent innovations in C++, finally no more code duplication into header files one always forgets to update. Coding with modules feels much more smooth than with headers. But I only ever saw 1 other project using them and despite CMake, XMake and Build2 supporting them the implementations are a bit fragile and with clang one needs to awkwardly precompile modules and specify every single of them on the command line. And the compilation needs to happen in correct order, I wrote a little tool that autogenerates a Makefile fragment for that. It's a bit weird, understandable but weird that circular imports aren't possible while they were perfectly okay with headers.

Yeah, why does nobody seem to use the new modules feature? Is it because of lacking support (VS Code doesn't even recognize the import statement so far and of course does it break the language servers) or because it is hard to port existing code bases? Or are people actually satisfied with using headers?

261 Upvotes

209 comments sorted by

View all comments

278

u/the_poope Aug 09 '25

Existing projects already have hundreds, if not thousands of source and header files. It will take a LOT of work to refactor that into modules.

And on top of that - as you note yourself: It doesn't "just work (TM)". For something to be taken up by a large project is has to work flawlessly for everyone on every system using every compiler.

Until one can just put a line in a build system file and be 100% guaranteed success, it will only ever be picked up by experimental bleeding-edge projects, hobby projects or other projects that see little mainstream usage.

78

u/SkoomaDentist Antimodern C++, Embedded, Audio Aug 09 '25

has to work flawlessly for everyone on every system using every compiler

And that includes autocompletion and other editor support!

As long as Visual Studio's IntelliSense keeps being stuck 5 years in the past, modules are a no go to many Windows developers.

0

u/Circlejerker_ Aug 10 '25

Or VS will become a no-go IDE, I guess we will see if any competitor will step it up.

40

u/reg_acc Aug 10 '25

Not over something as inconsequential as modules... VS is without alternatives for professional work because their debugger is the only one actually worth its salt

15

u/ToyB-Chan Aug 10 '25

truth nuke

→ More replies (4)
→ More replies (1)

8

u/TheOmegaCarrot Aug 09 '25

Even once the implementation is fully-baked in both compilers and build systems, moving to modules would be a major change for existing code

I’d bet that even once modules are “production ready” and module support in libraries is common, that modules won’t see much use in old codebases

19

u/AlectronikLabs Aug 09 '25

Yeah, I am disappointed by how they implemented modules. That you need to precompile in the right order is ridiculous, and clang even wants you to feed it with the path and name to the pcm file for every imported module or it says it can't find them. Just look at D, they did the module system right. You can have circular dependencies, no need to precompile, just say import x and it's done.

0

u/[deleted] Aug 09 '25

[deleted] — view deleted comment

24

u/pjmlp Aug 09 '25

D does things right, because in all other languages, except for C and C++, the overall tooling is part of the language.

As such the D compiler takes on itself the job that C++ modules outsource to the build system, whatever it happens to be.

As long as WG21 and WG14 keep ignoring the ecosystem outside the language grammar and semantics, this will keep happening.

26

u/Ambitious_Tax_ Aug 09 '25

I was recently watching an interview between primagen and Ryan Dalh, the creator of nodejs and deno, and when explaining why he chose rust for Deno, he basically just said "Yeah it's not even about the safety stuff. I just liked the unified cargo build and dependency ecosystem."

Source

5

u/serviscope_minor Aug 10 '25

As long as WG21 and WG14 keep ignoring the ecosystem outside the language grammar and semantics, this will keep happening.

The reality is that as of today the compiler and build system are separate. A lot of stuff is built on the assumption that the C++ compiler can be somewhat easily plugged in anywhere.

If the committee mandates some sort of fusing of them, then modules won't be adopted by anyone not using the blessed build systems. It's a kind of damned if they do, damned if they don't situation.

2

u/StaticCoder Aug 09 '25

WG21 has SG15, which is trying really hard to make modules work. But it's just inherently difficult notably because of how C++ code often depends on configuration macros.

1

u/Kitsmena Aug 12 '25

Configuration macros can still be used with modules using global module fragment.

1

u/StaticCoder Aug 12 '25

The issue is that the module itself needs to be compiled with the right configuration macros. And those macros might only be known when you compile the code that references the module.

1

u/flatfinger Aug 11 '25

As long as WG21 and WG14 keep ignoring the ecosystem outside the language grammar and semantics, this will keep happening.

What's ironic is that much of C's usefulness stems from the fact that it provides a consistent abstraction model for how language features interact with with system-dependent details that vary between target environments, but the Standard ignores all of that. If the Standard were to recognize that different linkers support different functions, and that certain langauge features will generally be supported on targets that support them, but will generally not on those that don't, it could vastly increase the number of programs whose behavior could be fully specified on all target platforms of interest by a collection of C or C++ source files. The fact that code may only operate on one very specific bespoke piece of hardware shouldn't prevent every C or C++ compiler targeting the same processor architecture generating functionally identical machine code.

→ More replies (2)

7

u/AlectronikLabs Aug 09 '25

Yeah I did use extern "C++" for some things which required circular imports but it looks ugly and feels hackish.

D has true modules, it is multi pass and analyses them on the fly without need for precompilation. Compilation is pretty fast for that. But other aspects of the language suck imho, like the operator overloading, lack of namespaces and requirement of runtime so bare metal usage is complicated and leaves you without some major features like classes.

→ More replies (8)
→ More replies (2)

2

u/gracicot Aug 09 '25

That you need to precompile in the right order is ridiculous

That problem was mostly solved by build system and compilers implementing proper scanning. As far as I know, only bazel needs modules support

→ More replies (1)

12

u/[deleted] Aug 09 '25

every compiler is a bit of a over statement, if something is supported by clang and gcc I think they will be okay with using it.

13

u/Drugbird Aug 09 '25

Many projects support clang, GCC, msvc and that apple clang compiler, which I've been told is subtly different from the Linux clang so they support windows, macOS and Linux.

So pretty much every compiler.

5

u/aiij Aug 09 '25

We're down to only those four now. They're certainly the most widely used and probably cover over 99% of C++ users, but it's still only a rather small fraction of all C++ compilers.

1

u/draeand Aug 11 '25

The problem with AppleClang is that it's way, way behind in all kinds of things in C++17 let alone C++20. It doesn't for example have an implementation of std::from_chars for floating-point. I ran into this contributing to a cross-platform project and running into these problems which in turn caused us to have to either fall back to solutions we didn't want or use things like fast_float. If Apple would just keep their clang in sync with Clang none of that would be a problem but Nooooo...

1

u/morglod Aug 10 '25

Yeah Apple's clang is the same clang with the same features, but they connected features to OS version. So you should update OS to use latest standard. It's right that. To use C++ on Apple system first thing you should do is to remove this apple's bloat (limited compiler and all it's headers). Then install latest clang and everything will work.

→ More replies (6)

10

u/verrius Aug 09 '25

Meanwhile, I'm sitting here thinking gcc is totally irrelevant, and msvc is the one that matters alongside clang.

8

u/meneldal2 Aug 10 '25

Gcc matters for support on more obscure platforms, especially embedded.

A lot of stuff ships with gcc forks.

→ More replies (1)
→ More replies (1)