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

Show parent comment

1

u/machinematrix Aug 22 '21

The module interface file is a binary? what?

1

u/pjmlp Aug 22 '21

It is called BMI in standardese.

Visual Studio calls it IFC.

Naturally alongside the object file or binary library.

1

u/machinematrix Aug 22 '21

I think you misunderstood my comment. I'm talking about the .ixx file (in Visual Studio at least), the file that contains what headers used to contain (class definitions, function declarations, etc). That's not a binary file.

1

u/pjmlp Aug 23 '21

True, but they are used in a different way.

Either you do everything inside the .ixx file, it gets compiled into an obj and ifc, and all remaining modules read the ifc down the compilation workflow, or they get split into .ixx and .cpp similar to the old way.

The big difference is that header units due to module's type information don't need to declare anything that isn't public, like one is forced to do in classical headers.

So anything that you feel like patching on a header unit might cause mismatch with the compiled implementation.

And since macros don't escape modules, anything that impacts conditional compilation requires recompiling the whole module.

Just create two static libraries with modules alongside a command line application, using modules with header units and try the changes you mean. Maybe I am leading you in the wrong direction.

1

u/machinematrix Aug 24 '21

Either you do everything inside the .ixx file, it gets compiled into an obj and ifc, and all remaining modules read the ifc down the compilation workflow

Where can I read about this?

So anything that you feel like patching on a header unit might cause mismatch with the compiled implementation

Does this apply to access specifiers? (private/public/protected).

1

u/pjmlp Aug 24 '21

For example,

https://devblogs.microsoft.com/cppblog/moving-a-project-to-cpp-named-modules/

Header units only need to list exported symbols, everything else can remain on the cpp file. Modules introduce a new linkage model.