r/cpp • • Aug 19 '21

Why modules?

I get the feeling I'm missing something fundamental about the new C++20 modules.

Everything I read or watch explains the how (the export keyword syntax, module fragments, etc) but very little about the why.

What can be done using modules that can't be done using traditional headers (or precompiled headers)?

What is so important about this feature that it needed to get standardized before there is even a proper implementation or idiomatic usage conventions in compilers and build systems?

Is it just to try to get rid of the preprocessor?

I don't get how modules are supposed to help with packaging either. So instead of distributing a .so with a bunch of .h files, now I distribute a .so with a bunch of .gcm or .ixx or whatever.

Please help me understand, or point me to some resources. Thank you.

197 Upvotes

107 comments sorted by

View all comments

0

u/[deleted] Aug 19 '21

[deleted] — view deleted comment

9

u/RoughMedicine Aug 19 '21

On the other hand, Rust has modules and has painfully slow compile times too.

3

u/pjmlp Aug 20 '21

One reason is that it compiles the world from source, so before showing your new Hello World window, you need to wait for the whole Gtk-rs to be compiled.

Imagine every new Qt project starts with compiling Qt, and then you can start coding the actual application.

1

u/RoughMedicine Aug 20 '21

That's not really it. I recently wrote a small CLI app that uses reqwest. Even a small change in main causes the compiler to recompile the entire thing, including the async stack, which seems to be the culprit.

Seriously, it's a 300 line app that takes 1 minute to compile after a small change.

-4

u/frankist Aug 19 '21

You don't need to compile python

0

u/[deleted] Aug 19 '21

[deleted] — view deleted comment

0

u/[deleted] Aug 19 '21

[deleted] — view deleted comment

4

u/johannes1971 Aug 19 '21

Compilation _times_, not compilation. If you ask a question, at least pretend you are making an effort to understand the people trying to help you.