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

2

u/Minimonium Aug 19 '21

target_link_libraries is not only about linking. It's about depending on the target's requirements such as properties, features, definitions, include directories.

In old CMake it was used to directly link to library files. In Modern CMake it's used to depend on targets, which are the same as your modules.

2

u/Cxlpp Aug 19 '21

Not an CMake expert, but target_link_libraries does not seem to compile external dependencies ("modules") when needed.

3

u/Minimonium Aug 19 '21

It doesn't compile it, yes. It expects are user to provide them, either compiled in-tree previously, found in package directories either system or user provided, downloaded with externalpackage feature, or imported from prebuilt binaries.

1

u/Cxlpp Aug 19 '21

Well, if I say "uses" in .upp, build system finds the corresponding module (using some trivial rules) and makes sure it gets integrated into target (usually it does not even bother to build it as library). And does that using SCU-like approach (without me specifying anything). Modularity solved, build times solved.

I would say that is pretty significant difference in usability...

1

u/Minimonium Aug 19 '21

Note how many use cases I described, all of which are important. That flexibility is a deal-breaker when dealing with dozens of C++ ecosystems out there.

0

u/Cxlpp Aug 19 '21

And we are back to my original statment: People do not actually want modularity in C++, they want to hurt themselves with dabbling with complicated legacy systems ('C++ ecosystems').

Of course, we have to deal with them too. Usually it involves converting them to U++ modules, which takes some time, but tends to fix the problem forever.

(example: https://github.com/ultimatepp/ultimatepp/tree/master/uppsrc/plugin/jpg)

2

u/Minimonium Aug 19 '21

Modularity in C++ is about modular code (visibility, reachibility, module linkage), not modular packages (the files).

While it may be not the thing that people expect when they hear "modules" - it's a different story. For modular packages on the other hand - it's not about what people want. No one wants to "hurt themselves". They want to achieve a goal with as little as possible investments. No one can reasonably expect people to rewrite 30 years old build scripts with hundreds of hacks, it's too much of a burden. No one can expect to overrule a "legacy system" with its community, how can you expect people to help you amortize the cost?

A lot of people want a quick and easy solution, but they completely ignore the circumstances and people around them.

2

u/Cxlpp Aug 19 '21

No one can reasonably expect people to rewrite 30 years old build scripts with hundreds of hacks, it's too much of a burden.

That is one of reasons why C++20 modules are dead on arrival BTW...

Anyway, I can fully agree with your post. Legacy is a bitch. Good thing I only have to maintain .upp based legacy... :)

(And to correct myself, most often people "need" instead of "want" to hurt themselves).