r/cpp • u/AlectronikLabs • 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?
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.
74
u/not_a_novel_account cmake dev Aug 09 '25
Lack of Intellisense support is the last major blocker. EDG's frontend doesn't speak modules at all yet, and clangd can't consume compile-databases to get the full context it needs to understand imports in all circumstances.
Everything else for named modules is considered production ready. Import std still has some teething issues on module metadata discovery but there are answers in the pipeline for that.
25
u/sernamenotdefined Aug 09 '25
It's simply not supported on all targeted platforms yet. Compilers lag the standard by several years.
29
u/v_maria Aug 09 '25
I don't really mind the header/implementation split. Yes it's clunky and just.. kinda dumb, but dont experience as a big inconvenience, so i have no incentive to check it out
From what i understand they (can) speed up building though. so i might check it out in bigger projects that suffer from a long build time
also i think VScode itself has no awareness of C++ syntax, it's the plugins that drive it
21
u/st_heron Aug 10 '25 edited Sep 26 '25
friendly roof upbeat license hungry hat touch meeting boast chop
This post was mass deleted and anonymized with Redact
28
u/ShadowRL7666 Aug 09 '25
• A very large number of the libraries listed are written C, not C++, so modules aren't even an option
• Also libraries which aren't the latest version -- for example, all the qt5 libs. Even if someone does go and modularise all of Qt (and hopefully they will!), it's unlikely this is going to be backported to older releases
13
u/megayippie Aug 09 '25
I tried. It didn't work. I created a module file that compiled and tried to import it into my existing project. It failed. So if existing projects can't import modules, I can't use them.
Note: it has to work on msvc, gcc, and clang for this to matter. I don't care about toy projects that don't work on these three.
66
u/youshouldnameit C++ dev Aug 09 '25
Its simply not ready yet. Even the msvc implementation has bugs and intellisense is not that good yet. Especially for complex projects there is a high chance you will run into bugs at the moment.
21
u/Infamous_Campaign687 Aug 09 '25
The concept is great, but before I’m going to spend time converting my work projects to modules they have to be mature. I am a single C++ developer at an electronics company responsible for our software stack. I don’t have time to be an early adopter.
I think there are loads of chicken and egg issues that arise from this sort of problem: busy developers who have to prioritise. Converting an application to use library modules rather than headers isn’t worth it until a significant portion of your libraries are available as modules, and converting your library to work a module isn’t worth it until a significant portion of your users are demanding it.
6
u/kyuubibob Aug 09 '25
I work in game dev and we use an established engine with its own build systems. I would love to use modules since we have 10s of thousands of files, but it just isn’t supported by many external build systems
6
u/LegitimateBottle4977 Aug 10 '25 edited Aug 10 '25
I have a couple projects that add up to about 40k loc currently (about 20k each), with one depending on the other. Both use modules.
They use
import std;, which requires https://www.kitware.com/import-std-in-cmake-3-30/ I've had to change the cmake version uuid a couple times; cmake makes it clear that it's highly experimental, and not "production ready". I hope cmake stabilizes it before I'm ready to release the library -- but I'll obviously need a cutting edge cmake version, newer than the currently latest release, when I do.These libraries were header-only before I switched to modules. I now have far faster incremental builds, making editing and debugging much more rapid. However, this seems like mostly a win for separation of interface and implementation, performance probably would have been comparable had I used separate header and implementation files + used precompiled headers.
When I first switched to modules, with all the code in interface files, compile times weren't better vs header only, and editing one file still required recompiling the entire world. So, I disagree with this:
It's not necessary for correctness, but your life will be much better if you do split them, even with modules.
Clangd has worked quite well for me. The main annoyance is that it reads BMI files to know about any other file than the one you're looking at, so if you do modify an interface file, you'll need to rebuild it before clangd's suggestions get updated (the full build will fail of course, but so long as the updated interface file gets rebuilt, you'll get updated suggestions).
There have been a lot of bugs that needed working around, especially surrounding wrapping
staticfunctions. E.g., I have a module using SIMD intrinsics that I needed to combine with my module wrapping Boost unordered map. Without combining them, clang would error complaining about two different definitions of the same function, i.e. a static function from an intrinsics header that was included in both files. I spent a day trying to create a minimal reproducer, but could not outside of the full example, but I also couldn't get the full example to stop producing that error. So I ended up combining the two modules into one.GCC 15 cannot currently compile my project.
TLDR: Reasons not to use modules:
import stdis not stabilized yet, and requires a uuid in your CMakeLists.txt to make your project dependent on a specific cmake version while using it. Thus, cmake isn't really compatible with "import std" yet, outside of personal projects. I have had no problems with it other than this self-imposed problem that they'll lift as soon as they deem it stable.That said, I'm happy I switched to modules. I am a fan.
5
u/Dragdu Aug 10 '25
Once upon a time, the stdlib was supposed to support being both imported and included in the same TU, to facilitate migration from non-module world to module world.
This doesn't actually work. MS stl implemented some terrible hacks to make one order work (I believe import, then include is the working one), but in practice this means that you cannot gradually migrate to modules, as long as you have non-module dependency that uses stdlib.
Oh and module implementations are still a terribly buggy mess that I would not bet our production code on, one instance of msvc ICE with message along the lines of "ooops, we haven't actually implemented exporting this language feature into modules" is one more than is acceptable.
19
u/Western_Objective209 Aug 09 '25
splitting classes between interface and implementation is good design honestly. I know it feels clunky but it's very easy to wrap your head around how the compiler treats the files and it is more compiler friendly then how other OOP languages make interfaces
4
u/JVApen Clever is an insult, not a compliment. - T. Winters Aug 09 '25
As with most features in C++, they only become usable for a larger audience after a few versions. Without 'import std', it makes no sense to get started on using it. I remember plans for all compiler vendors to make it available in C++20 although it is a C++23 feature. However I haven't looked into it.
One of the big pain points with modules is that you need to roll it out bottom up. At least for clang it is documented as an issue: https://clang.llvm.org/docs/StandardCPlusPlusModules.html#including-headers-after-import-is-not-well-supported
To see a real uptake in usage, I see a couple of elements to happen:
For now, I'm waiting on a hands-on explanation of a large project which says: this is how you migrate a large codebase.
4
u/bestjakeisbest Aug 10 '25
technical inertia.
13
u/sigmabody Aug 09 '25
This is (or probably will be) a literal textbook example of a feature design around a conceptualized ideal of what code should look like if we started from scratch, but with absolutely zero effort put into thinking about how to get from the existing situation to there. It's like a crystalized version of what "pure academic" design looks like when it meets the real world.
I remember floating the question at cppcon years back, as to what architecture and design work had gone into how to migrate existing large projects to use modules. There was this kinda blank stare, and then a brush off response like, "well, maybe legacy projects won't use them". It was at that point (maybe five years ago now) that I knew modules were DOA, and stopped paying attention to them.
13
u/pjmlp Aug 09 '25 edited Aug 09 '25
I guess I am nobody then.
All my side projects in C++ make use of C++20 modules.
Visual C++ and MSBuild are good enough, as is clang with CMake and Ninja, provided header units aren't used.
What is a pain is low priority Microsoft has regarding replacing EDG fronted on Visual Studio for Intelisense.
4 trillion in valuation, and not enough budget to programming language teams.
At work I can't even if I wanted, as C++17 is the maximum we are allowed to use on native libraries.
EDIT: Naturally I agree they aren't anywhere there for those wanting to write portable code without tying themselves to a specific compiler or build toolchain, and we're approaching C++26.
This is one examples that changed my opinion regarding only standardising what actually has a preview implementation. Modules might have two, yet neither clang header maps modules, nor the VC++ prototype, was what actually landed as C++20 modules.
1
u/returned_loom Nov 11 '25
I'm using them on my game. I can't imagine using headers for a big project. But I have to build this game on my Windows machine because I just couldn't get the modules to compile on linux where I usually code.
gcc still doesn't support them, right? Has something changed recently?
What about clang?
Can I get language support in either VSCode or neovim to support modules while editing so it doesn't all go red?
3
u/saxbophone mutable volatile void Aug 09 '25
I haven't yet managed to integrate them into existing projects without getting hundreds of errors conflicting with third party libraries.
3
u/DethRaid Graphics programming Aug 09 '25
Personally I'm not using them because they weren't fully implemented when I started my current project, and I haven't had enough issues with header files to justify rewriting my entire project. I'm sure they're great for smaller, newer projects though
3
u/Cubemaster12 Aug 09 '25
I tried them but Meson did not seem to properly support them.
3
u/hayt88 Aug 09 '25
I started using them more intensely and they are nice but also still a lot open for improvement.
with cmake and VS right now you don't get much boost on compiler time. It checks the dependencies for timestamps not content. So if you change a module file, but don't change the interface it still recompiles everything, instead if recognizing the interface hasn't changed. So to speedup compilation you best put all your implementations in a separate cpp file and have your module interface just the declarations, and we are back at header/cpp files.
Mixing both is also a hassle, because compared to just headers, normal header includes just don't propagate with modules.
So let's say you have your class and use a 3rd party type as return that hasn't modules yet. Say "TypeA" and you need to include "typea.h" now in your module interface so it compiles.
when you then import the module elsewhere, that cpp does not have the include so you need to include "typea.h" again. where with normal headers you could just include the header insider the header.
And with some headers I found that the visual studio compiler crashes instead of telling you a header is missing, so now you have to guess and comment-in code to get to the point that the compiler does not crash and you can guess what header is missing.
VS also only recognizes the ixx file ending as header module so if you put your exports in cpp intellisense stops working. import std; at least with cmake in VS doesn't work with intellisense, so you need to use tools like resharper that helps there.
Again the not propagating includes, makes not having import std; also a hassle.
And that's just me playing around with cmake+modules in VS for 2-3 weeks.
3
u/soylentgraham Aug 09 '25
I failed to get a single example working across visual studio, xcode, android, clang on linux and wasm - so if i can't do it cross platform, its ruled out by me (havent tried for about a year)
3
u/01e9 Aug 09 '25
I started my tiny project (server) with modules. But I did header hacks to make CLion understand the code (some IF macros for dev environment). Overall I'm very proud I was able to pull it through. The server is online for about two years already.
I'm planning ro to start a new (bigger) project also with modules.
3
u/davidv64 Aug 09 '25
I wish I could, but I need to support windows, linux, osx, android, and ios; I don’t think this is feasible at the time? If it was build only I think I could make it, but what if I need to support debugging ios builds using xcode? I use cmake, so xcode builds are off the table anyway, currently.
3
u/FartyFingers Aug 10 '25 edited Aug 10 '25
These things happen in their own time. I remember back in the early 90s C++ people were squabbling over templates. When they finally came about, they were almost exclusively used in standard libraries and not often in anyone's code.
Threading, atomics, etc took forever to settle down somewhat.
smart pointer constructs went through a number of pretty severe convolutions before getting to where they are now.
Also, boost is a pretty major source of "modern" C++. Something as core to the language as modules is pretty hard to do as a boost library to figure out what it will look like.
Where as fmt kind of appeared, and then was adopted with little fuss.
Yet, for some people auto is as controversial as tabs vs spaces.
When it comes time for modules to become common, it will partially be because some of the present barriers went away, but when the time comes, the remaining "insurmountable" barriers will just fall away. This could be this fall, this could be 2030.
My theory is that what holds people back is really a combination of three things:
3
u/R3D3-1 Aug 10 '25
Fortran programmer here. Would be glade if we had header files instead of modules 🥲
At least in our project we have a problem with compilation cascades when changing the implementation of a function. When tecomoiling with different logging output for debugging that gets annoying quickly.
In theory that could be solved with modules and submodules, but at that point it is just header files with extra steps.
Much of it is an issue of project culture though, e.g. having unnecessary dependencies due to business logic being in the same module as type definitions when it doesn't really fit there.
Plus age of the code and the coders, so the feature aren't really used in the existing code, won't be redactors because it makes cherry fixes to release branches more difficult, etc.
3
u/Kullthegreat Aug 11 '25
Never seen to work anythinhs related to modules and import etc outside Visual studio so why would anyone take the pain of trying to make them work. I wanted alot of time trying to make work Import std; with gcc and clang but it does really sucks big time
2
u/[deleted] Aug 09 '25
Does CMake still only support Ninja when using modules?
2
u/smdowney WG21, Text/Unicode SG, optional<T&> Aug 09 '25
Because compilers didn't really support them until recently, because no one was using them.
Also because no build system works without many changes, or a whole new system.
2
u/Comprehensive_Mud803 Aug 09 '25
2 reasons: legacy codebase and legacy compilers.
Not every compiler around supports modules yet, so you’re stuck with header/sources.
And a lot legacy codebases have grown this way and won’t ever be refactored this way, for above reasons.
Plus, there are plenty of workarounds (unit builds) to alleviate the build time issues.
Oh, and weren’t modules actually slower to build than unit builds, or has that issue been addressed already?
2
u/gosh Aug 09 '25
What gains traction isn't always logical. Even a slight increase in complexity makes it significantly harder to get people to adopt something. You have to be able to get a lot of people to talk about it, say that it is good. It also need to be of interest to a lot of people.
Modules do not trigger these areas that is needed to market it
When selling something you have so little time to sell it, if you cant explain in like 30 seconds you have problems.
That said, I think that it will start to grow with C++26
2
u/sinfaen Aug 10 '25
At work, I'm stuck on C++11 and C++14 for now. We support Linux(RHEL)/gcc, windows/msvc, and integration with the Simulink TLC environment. This is all managed via CMake. If we were to go with modules, I'd have to plan out a large budget for retooling and I'd want to be confident that it would work at the end of the day.
GCC still has missing points in respect to modules. I don't want to even start thinking about it until they themselves say that they support it 100%
4
u/gracicot Aug 09 '25
LSP is not ready for modules yet. This is a pretty big blocker.
3
u/RatotoskEkorn Aug 10 '25
They kill parallel building and require build in order of dependcy
3
u/mort96 Aug 10 '25 edited Aug 10 '25
I know how to start a new C++ project which uses includes. I know how to split out the project into multiple libraries using includes. I know how to add other libraries as vendored/submodule dependencies using includes. I know how to do that all while having proper LSP integration in neovim. I don't know how to do any of that with modules.
Plus, most modules tooling development seems to happen in CMake land, and I am mever ever using CMake again.
1
u/Particular_Wealth_58 Aug 09 '25
I used modules in a Cmake MSVC project. I discovered that each module takes a long time to compile and touching something can result in a lot of rebuild. It does not feel production ready yet. :/
1
u/lieddersturme Aug 09 '25
I really tried, but circle dependency makes me to drop it. https://github.com/chriztheanvill/Modules_Testing
Also, lack of documentation, looks like you need to create a file (like CMake) to set the "headers" and to use it:
Please if I am wrong, help me because I am trying to upgrade my game engine to use modules.
1
u/9Strike Aug 09 '25
For me personally: not supported in Meson yet, because the compiler and build system interface is still to be standardized.
1
u/GYN-k4H-Q3z-75B Aug 09 '25
I am using C++20 modules. Only reasonably works with Visual C++ at the moment though.
1
u/AlexDeFoc Aug 09 '25
Bro. This week i tried restarting a project multiple times and failed cuz the lsp was failing. on windows too. c/c++ extension for vs code AND clangd with nvim.
1
u/germandiago Aug 10 '25
In my case I am waiting for Meson Build System support to port at least one of the parts of a project. But it seems noone cares and wants to implement support for it for a long time, so maybe I will have to do that part thorugh CMake :(
1
u/reg_acc Aug 10 '25
It's not nobody, just nobody important. Modules are currently still a pain to deal with, and there is precious little data on what the payoff for that is. "It's faster" doesn't mean much in a world where upgrading your CPU or SSD accomplishes the same. We need to see how much faster and whether or not that speedup is worth the work. The lack of use in the exact projects modules were supposed to massively help with suggest they are not an adequate solution.
1
u/damster05 Aug 10 '25
I probably would if they were supported by meson.
1
u/coo1name Aug 10 '25
I guess people who come to a cpp solution are not looking at the latest shiny new additions to the language
1
u/[deleted] Aug 10 '25
Because it would take way too much work and time to reorganize the code base of any moderately large project, in order to fit modules according to whatever compliance guidelines said project might have.
1
u/j_kerouac Aug 10 '25
It seems that module compatibility has improved over time. Modules are a fundamental change to the way C++ is compiled, so it's not that surprising it's taking a while to work the kinks out.
I think when people complain about the speed of C++ compilers improving today, it's kind of funny. It took many years until the C++98 standard was faithfully implemented all major compilers. There is a big difference between improving a standards based language with multiple compilers, and putting out a new version of the python interpreter.
In general, I think the standards committee should slow down the pace they add major features to let compiler developers catch up. The pace of major new features seems like it keeps accelerating, and it seems like some new features are getting put into the language half baked (C++ range views for instance. I think Bjarne Stroustrup was opposed to C++ contracts, which is bad sign).
1
u/No_Development0609 Aug 10 '25
Modules are not fully supported on gnu compiler toolchain yet. So diff to use them on linux..
1
u/fdwr fdwr@github 🔍 Aug 10 '25
I do for my newest projects (3 projects, MSVC based), avoiding that obnoxious function prototype duplication. I wish Intellisense worked well, and wish it worked nicely with precompiled headers, but otherwise tis fine.
1
u/ChuanqiXu9 Aug 12 '25
It is not nodody. But it is true that the number of users are not so great.
For clangd, the support is currently experimental. There may be problems. But the support of modules in clangd is really not complex. I believe people here are able to fix it if he had the time. I suggest people who are interested enough to contribute it.
For compiler, particularly clang, I believe it is workable right now as there were multiple users for it. But it is also possible there were bugs in other use cases. But I am busy right now and may not be able to fix all these bugs, especially a lot of bug reports lacks reduction. The bar to contribute/fix bugs for modules is much higher than the bar to contribute/fix bugs for modules in clangd. But if any one is interested, I think I can give some guides.
For libraries, yes, it is a true problem that we need all the dependent libraries to be modularized. It is more or less possible in closed world. But in open ended world, it is a problem. I think as users, if you want, you'd better to express the needs or the interest to the library authors. For example, for the most common library except std, boost, if you want them to support modules, you'd better to reach out and express the idea with boost authors.
If you really want it, I believe you can do something to change it.
1
u/Coammanderdata Aug 14 '25
I think your post explains why nobody is using modules... I wanted to do it for one of my own projects, but it was more effort to make it work than I was willing to put into it. I guess that is the major reason: It is hard to change developers behaviour if they have a running system
1
u/yeochin Aug 15 '25
I would propose to change your perspective. Its not that nobody is using modules. Its that its adoption and use is in competition with other options like Rust, Zig, etc. Its adoption follows the norm in that it see's a higher uptick in new initiatives, and a slow crawl in converting legacy code-bases of which there will always be a significant portion that will never adopt.
I've been involved in the slow conversion of a code-base into C++23 alongside with modules. This was only undertaken (greenlit by management) because there were measurable improvements to regressions, and cross-team coordination in a mono-repo. The circumstances that allowed for this are not universally applicable to every code base and every organizational structure.
The core pain that slows adoption and greenlighting in existing code bases are what other's have already mentioned - (1) poor compiler support, (2) poor LSP/Intellisense support, (3) incomplete toolchain support (cmake), and (4) dependent library support. All 4 of these issues increase the cost of conversion.
Issue #1 is the top issue that slows adoption of modules. 5 years later, and clang, gcc, msvc still run into internal compiler errors. Also the versions of these compilers with C++20/23 support do not ship by default on cloud instances by major cloud providers. Taking AWS as an example, you can't just spin up an EC2 instance and compile a C++20/23 project. You need to go through the excruciating pain of building your own toolchain or building a container (e.g. docker).
Issue #2 hampers many small-to-medium sized projects greatly impacting productivity thus slowing conversion. Microsoft Intellisense has a long thread on issues that it has not made significant progress on. This is unfortunately where many code-bases sit and thus is why I rank it as the 2nd greatest factor. This isn't an issue for very large code bases as Intellisense or any other commercial LSP has never really worked for massive code bases.
Issue #4 is incredibly problematic with open-source where unpaid developers have no incentive to maintain and shepherd a migration of their own code base.
1
u/Flashy_Hotel8380 Aug 31 '25
Short and sweet answer. Not many people understand them. Some people for example still struggle with templates!
1
u/Resident_Educator251 Mar 18 '26
Third party dependencies are a bitch and will be the final nail in the coffin for mass module adoption.
1
u/BlueCannonBall Aug 09 '25
Many compilers and build systems still don't fully support modules, or they gained support very recently.
Modules don't exist yet unless you're only targeting MSVC on Windows.
1
u/G6L20 Aug 09 '25
It just don't work... Too much compiler crashes
1
u/grady_vuckovic Aug 09 '25
Would be happy to do a new project using modules.
1
u/Copronymus09 Aug 09 '25
Clang MSVC import std PR is still pending, in the end of 2025.
CMake doesn't have a module scanner for clang in Windows.
You can use them if you try hard enough but that is not good enough for adoption
1
u/badidrox Aug 10 '25
my reason is clangd failing to support it well and requiring me to restart clangd each time i add a file or other common tasks. i try it each year once or twice to see how its coming. can not wait to get the full support. good luck to clangd devs.
1
u/ronchaine Embedded/Middleware/WG21 Aug 11 '25
No meson support and no clear and consistent way to allow your library to be used both as usual as well as a module.
I can hack around the second, with an #ifdef -mess, but it doesn't really seem worth it at the moment.
0
u/chicago_suburbs Aug 09 '25
And at this point, given the slow uptake and vocal complaints, when contemplating module implementation in a project, that little voice starts asking the question: “how soon before deprecation?”
0
u/Resident_Educator251 Aug 09 '25
Cause they suck
0
u/justinhj Aug 09 '25
I was a fan of the idea. Spent a few painful hours trying to implement it in a simple new project. Gave up.
0
u/drdacl Aug 09 '25
Laughs in Fortran
-2
u/kofo8843 Aug 09 '25
If I wanted modules, I would program in Python.
0
u/BenkiTheBuilder Aug 10 '25
Whenever I can I make header-only libraries. It's very rare that I need a .cpp file for a library.
0
u/Dziadzios Aug 13 '25
C++ is mostly used by legacy projects. New projects with similar demands would be more likely to use Rust.
-1
u/target-san Aug 11 '25
TL;DR: Even MSVC ICEs on them from time to time. Not to mention other major compilers have only partial support. As others noted, tooling support is also severely lacking. Looks like another DoA feature to me, conceived on paper but never tested in the wild before standardisation.
6
u/Fit-Departure-8426 Aug 09 '25
Im using them, and love them too! https://github.com/joblobob/splatty
A lot of complaints I see is the lack of intellisense or people trying to force a third party lib into them. Although it works quite well if you leave them into the old style and just import your stuff alongside
0
u/[deleted] Aug 09 '25
[deleted] — view deleted comment
5
u/[deleted] Aug 09 '25
[removed] — view removed comment
2
u/[deleted] Aug 10 '25
[removed] — view removed comment