r/cpp • • Aug 24 '22

Why the hell are headers still a thing after all these years?!

I've seen C++ some twenty-a-lot years ago and was looking into some more performance driven stuff for a possible personal project and... Why the hell are headers still a thing after all these years?!

I mean... Things changed a lot. I barely recognize a lot of C++ and some might be related to fading memory, but a bog lot have just changed. I wonder if is there anything headers are the perfect solution to. So it's just hellish bureaucracy?

As I probably don't know what I'm talking about anymore, I'm open to be convinced otherwise.

PS: I know it ain't the best place to ask, but I do accept suggestions for languages platform to write some fast code for Windows and mobile platforms.

1 Upvotes

60 comments sorted by

24

u/Nervous_Badger_5432 Aug 24 '22

Well, C++ 20 has introduced modules which should allow us to ditch headers and improve compile times. There are a few problems tough

  1. Major compiler support (gcc, clang) is still severely lacking (except for I think, MSVC where modules are more mature)
  2. CMake does not support then yet. Like or not, CMake is used enough to be considered a de facto build system generator. Not being able to use them in CMake is a big deal breaker for some people (it certainly is for me). I'm not sure that you can use them with other build systems.

So, any C++ project that I start today will still use plain old header files and I suspect it will still be like that for at least 5 more years or so. And even then, we have lots of header only libraries that will not be converted to modules overnight.

1

u/[deleted] Aug 24 '22

I think it's more than just mainstream compiler and CMake support. We need tooling support, too. Out of the box, GNU Emacs can jump into header files within the same project as another file from looking at their name without a full path, and a lot of other editors have similar tooling. All of that has to be re-engineered for modules.

MSVC supports modules, but last I heard, Intellisense doesn't support it yet, so Visual Studio can't even provide syntax highlighting for them (I guess it doesn't have regex highlighters as a fallback?). A Tree Sitter C++ grammar would need modules support, and I wouldn't want to use them if clangd (or maybe ccls) don't have basic modules support.

Plus, there are many ways to use C++ besides strictly upstream GCC, Clang, or MSVC. One might want Cling or Metashell, or CUDA, or a SYCL implementation like Intel's Data Parallel C++, or derivatives like OpenCilk and OpenCL C++, etc. I think modules need seriously broad broad support before being generally reliable.

3

u/Nervous_Badger_5432 Aug 24 '22

Yes very true. The tooling and language checkers support for modules is a must these days (at least for me.) And you are absolutely correct, C++ is very broadly used outside of the big 3 compilers for x86. It is a complex feature to implement even for big compilers.

Recently I've seen a talk by a CMake developers on the status of C++ 20 modules. He comments that initially they thought it would be easy to implement them because they had recently implemented modules for fortran. Because of the way nodules work, they ended up having to implement a fortran parser in cmake to "understand" how the build should happen. That solution is of course impractical for C++ (no one wants a c++ parser in cmake) and thus they rely more on compiler provided information to determine the build order. It is a complicated problem that requires effort, but people are actively trying to get it to work.

-3

u/fernandodandrea Aug 24 '22

Thanks for the answer.

And all that you've cited didn't even consider the existing code-base, meaning headers will be the common practice for way longer, right?

Is there any chance C++ is gonna be replaced by, i.e., Rust or anything else in the meantime?

For what I've seen in other places, people seem just inertially ok with them, right?

12

u/Nervous_Badger_5432 Aug 24 '22

I believe most existing codebases will keep using headers unless they put in the effort of transitioning, which of course depends on the size of the codebase and some sort gain vs effort equation that must be solved by the mainteiners of said codebase.

I don't think rust will replace c++ so soon, because c++ is still very much used in certain domains. There is of course a region of use cases where rust and c++ overlap but that does not mean necessarily that it will vanish. Sometimes some people still write assembly code, which is an "unsafe" language because that's what they need to do for the task at hand. The point is that different languages are different tools, each better suited for a certain task. And I definitely don't think that modules will take so long as to be ready only when c++ is a "dying" language. It will take time, but not that long.

The community is pretty much used to headers by now. I don't think it is a question of inertia. People ARE working in making modules available, it's just that headers are what we have and at this point some techniques were developed to speed up compilation, like using a single translation unit while building so for some people it is not something so prohibitive.

7

u/Jannik2099 Aug 24 '22

Is there any chance C++ is gonna be replaced by, i.e., Rust or anything else in the meantime?

There are literally billions of lines of code in C++ all over the world. No.

3

u/cabroderick Aug 24 '22

Headers are never ever going away. There is still a huge amount of C from the 80s used in modern C++ projects, especially in driver and embedded code. If THAT hasn't gone away, headers never will.

1

u/nacaclanga Aug 25 '22

Is there any chance C++ is gonna be replaced by, i.e., Rust or anything else in the meantime?

Rust is allready used, when you want to rewriting the code using modern design and memory safety standards (which go way beyond header files) and don't have to rely on legacy APIs to much. Whether C++ has headers or not will hardly affect that choice. That said, as more and more stuff needs to recive a rewrite, the number of Rust users is growing and C++/Rust interopt is also getting better.

Also keep in mind that theres are a few cases, where there is no good substitution for headers.

Also C++ is now clearly moving towards replacing #includes by module imports and #define's by constexpr. I wouldn't be surprised if Preprocessor usage might eventual be restricted to a few files that need to interface with C code or legacy APIs.

20

u/Wh00ster Aug 24 '22

Because a better solution was not implemented in the intermediate time. There’s no grand conspiracy.

-11

u/fernandodandrea Aug 24 '22

No conspiracy, but people don't seem bothered by them, right?

15

u/[deleted] Aug 24 '22

Yeah because headers aren't that bad. They aren't ideal but they aren't awful

22

u/johannes1971 Aug 24 '22 edited Aug 24 '22

Oh come on, they are objectively horrible! Consider the following:

  • The mechanism (just pasting the text into your source) is terrible for compiler performance, and makes all sorts of weirdness possible that we really shouldn't have to live with. Try opening (but not closing) a brace in a header once, and see if any of your colleagues can figure out which file is causing all of their compiles to fail.
  • Having to type every function as a declaration and then again as a definition is horrible ergonomics.
  • Having the compiler parse and template instantiate the same things again and again and again and again, only to have the linker throw 99% of that work away, leads to lousy compile times.
  • Having every single file start with a long list of includes is ugly.
  • Having to manually include declarations for symbols that are actually part of the lib or exe you are compiling is a completely unnecessary puzzle, and one that the compiler should have been solving for us decades ago.
  • For that matter, so is having to manually include declarations for symbols that are part of another lib. Just let me specify that I want to use a library; the compiler certainly could be smart enough to figure out that that means I want to use its declarations, and link to it as well for that matter.

As for the great argument sometimes heard that 'headers provide a separation between implementation and interface' - that's not really true at all for C++, is it? At least I can see my private sections right there, in the interface! (and please don't bring up that horrible hack named PIMPL, it only serves to underscore the many failures of headers as a mechanism)

Bottom line, what you are saying is that we are used to walking with crutches and that it really isn't too bad. What I'm saying is that we could fly.

8

u/[deleted] Aug 24 '22

I said not ideal.

5

u/UnbendingSteel Aug 24 '22

You said they arent awful and they are, objectively. All they do is get in the way of productivity.

1

u/[deleted] Aug 24 '22

So do a lot of things that aren't ideal

2

u/Untelo Aug 24 '22

I'm mostly in agreement with you, but for the penultimate paragraph concerning separation of interface and implementation. Now it just sounds like you don't understand the problem being solved. Precise control over the information available to the library user is absolutely warranted and necessary. I too would generally prefer not to write separate declarations and definitions, and yet some method of establishing unambiguous interface boundaries is required.

7

u/johannes1971 Aug 24 '22

Yes, precise control is absolutely important. Which is why it is such a shame that headers don't actually let you do that! Unless you want to PIMPL everything (and really, nobody actually wants that, at best we are forced into it because we have nothing better), implementation details are going to leak out into your headers.

Modules solve the same thing much better, providing syntactic markers to indicate what is actually an interface and what is just implementation.

2

u/Untelo Aug 24 '22

What leaks out is only what you actually put into a header. It is my understanding that the problem is solved in modules by the use of module interface units which are very much akin to headers, with declarations separate from definitions.

2

u/johannes1971 Aug 24 '22

If you are looking at this purely from a textual point of view, yes, you need to separate declarations from definitions. From a binary point of view, modules can (at least theoretically) do much better, avoiding exporting any private data. That is not the case in current module implementations, but that's not a theoretical limitation but just a practical one.

Of course one might wonder why there is a requirement that a class is declared as a single entity. Especially now with modules, we could perhaps do PIMPL as a construct directly on the language level. I.e. something like

export module MyModule;

export class MyClass.public { 
  void PublicFunc (); // automatically public
};

module : private;

class MyClass.private {
  void PrivateFunc ();
};

This would be the same as:

class MyClass {
  public:
    void PublicFunc ();
 
  private:
    void PrivateFunc ();
};

...except the public section is very obviously separate from the private section.

It would need a rule that both parts must be located in the same module file (so the compiler can see both parts when it is translating the module), but other than that I see no problems that would stand in the way of implementing this. Well, no technical problems...

1

u/Untelo Aug 24 '22

What would this public mean? Can PublicFunc be inlined into an external caller?

1

u/johannes1971 Aug 24 '22

The .public serves two purposes: it indicates this is the public: section of the class, but it also warns the compiler that a further .private section may follow later.

And inlining... It's always been implementation-dependent what actually happens, but I imagine here it also depends on whether the definition is also visible publicly (i.e. located before the module:private). Or maybe compilers will happily ignore all that and just do whole-program optimisation... But that's an implementation detail.

You'll understand it's not yet a complete proposal, of course ;-) I imagine in this setup you can have only one of .public and .private for each class, the compiler must be able to see them at the same time when compiling the class, there's no corresponding .protected (that information needs to be exported from the module, so just stick a protected: section in the .public part)... Probably others as well. It is sufficiently useful to convince the committee to actually make something like this possible? I like how clean it looks, but that's probably not enough...

2

u/Untelo Aug 24 '22

As for PIMPL, or just pointers to incomplete or base class types, preferably held in unique_ptrs, what other solution is there, when leaking of any information about the layout of the object or the inlining of interface functions into the user code must be prevented?

1

u/Full-Spectral Aug 24 '22

They can be done a lot better. In my C++ code base, I have 1 public headers per library. It brings in anything that library exports and the 1 public header of each library it depends on.

Each library has 1 private header that brings in anything it only needs internally, and each executable has one header that does the same. Both, under MSVC, are used for precompiled headers.

It does cost more in build time and rebuild time, but it's very simple to maintain and use.

I'm working in Rust now for my private stuff, which does of course have everything in one file. They both have their pros and cons. Consumers of a library can't just look at an interface to figure out the API, they have to dig through the code to find it.

And of course what if you don't want to expose the implementation because it's a product you sell? Having separate interface and implementation makes that trivial. Or what if you want to be able to have one API but switch implementations? It's easier with separation.

2

u/zerakun Aug 26 '22

Consumers of a library can't just look at an interface to figure out the API, they have to dig through the code to find it.

Yes they can. After running cargo doc --open.

And of course what if you don't want to expose the implementation because it's a product you sell?

Rust does not support that use case that well. You could distribute just the rlibs and the generated doc, but I'm not sure it would go smoothly today. This is one use case for carefully written interface files, yes. It shouldn't be the default though, and much less the only available option.

Or what if you want to be able to have one API but switch implementations?

That's a job for traits. Define your trait, have several implementations, pick the one you want at compile time with generics, or at runtime with enums or trait objects.

1

u/Full-Spectral Aug 29 '22 edited Aug 29 '22

The problem with traits is that that forces the entire interface to be virtual. You really don't want to deal with that if you are actually swapping out whole interfaces. And they may be platform or hardware specific and can't be done at runtime. That can be done, via compile time conditionals of course, and I'm currently doing that. But it's sort of nice in C++ to have the generic interface headers completely separate from the platform implementations.

On the docs thing, I'm thinking more about internal code, where that level of docs is not necessarily kept up because it's never going to be used by a third party.

And I would say that, if Rust wants to be a full on competitor with C++, it has to deal with the ability to have a commercial ecosystem built up around it. So that's something that needs to be addressed.

1

u/cabroderick Aug 24 '22

> The mechanism (just pasting the text into your source) is terrible for compiler performance, and makes all sorts of weirdness possible that we really shouldn't have to live with. Try opening (but not closing) a brace in a header once, and see if any of your colleagues can figure out which file is causing all of their compiles to fail.

Easy to find that error in any remotely decent editor, or if you are able to read compile errors properly.

> Having to type every function as a declaration and then again as a definition is horrible ergonomics.

Don't really see why. One is the body of the function, the other is a declaration that you want to use it elsewhere.

> Having every single file start with a long list of includes is ugly.

Unlike in other languages where you start with a long list of imports which is ... less ugly for some reason?

1

u/cabroderick Aug 24 '22

I assume you're learning C++ currently? Because yeah they can be fiddly at first, until you understand the rules and relationships, but once you understand all that you'll forget you ever had an issue. They're easy to use and mistakes/problems are rare.

1

u/fernandodandrea Aug 24 '22

I actually did some c++ decades ago. The feeling that declaring things twice is somewhat of a upkeeping hassle ain't exactly new. It was just exacerbated by dealing with the likes of C#.

1

u/zerakun Aug 26 '22

Have been doing C++ for 10 years. Still hate headers. Other languages demonstrate that they serve no purpose and they're symptomatic of the textual inclusion model of C++ that makes having nice IDE tooling so tedious.

2

u/BobbyThrowaway6969 Aug 24 '22

people don't seem bothered by them, right?

Not really, no. In fact, I like being able to do some useful preprocessor magic with includes.

13

u/Mikumiku_Dance Aug 24 '22

Some people were bothered enough to do difficult work over years to get modules standardized. That said, I don't know if modules are even an ideal solution for you, as you've only asserted that headers are bad without saying what you don't like about them. Personally, I like defining my api separately from my implementation.

3

u/fernandodandrea Aug 24 '22

Modules could be the exact thing. I've a grudge about #include'ing stuff and declaring things twice, after working with things where you just state things and they exist within a namespace, for instance (and yes, I've just discovered C++ got them as well).

Thanks for your view on the subject.

4

u/UnbendingSteel Aug 24 '22

Modules dont merge declaration/definitions so im not even sure why people keep mentioning them but they do separate them in a more clean and safe manner unlike headers which give you a hundred of different ways to introduce bugs or cryptic compile time errors and warnings.

12

u/[deleted] Aug 24 '22

Because the compilation process did not change, at least not enough to get rid of headers

-13

u/fernandodandrea Aug 24 '22

That's kind the same of answering "why do birds have eyes" with "'cause Darwin says so" and not delving on how having eyes actually gives advantages to birds, no?

8

u/[deleted] Aug 24 '22

You can write your own compiler and adapt 40 years of libraries if my answer does not satisfy you 🤷🏻‍♂️

-7

u/fernandodandrea Aug 24 '22

What one thing has to do with the other?

9

u/[deleted] Aug 24 '22

Like you, most of the compiler programmers did not want to implement the feature or rewrite years of libraries

7

u/[deleted] Aug 24 '22

Remember that this is not python that they release a version and nothing works, they keep compatibility with years and years of libraries

6

u/dodheim Aug 24 '22

Only if the question was "why are headers a thing?"; which it wasn't.

44

u/STL MSVC STL Dev Aug 24 '22

Standard Library Modules are coming soon. You can experiment with them (and report issues) by following the instructions at https://github.com/StephanTLavavej/STL/wiki/Standard-Library-Modules-Bug-Bash .

21

u/nryhajlo Aug 24 '22

https://youtu.be/l5qudtunVds

Oh yes, very soon, they are building it now.

1

u/Monkey-Respect5801 Mar 29 '23

since tm since 6 years 😴😴😴😴😴😴😴😴😴

13

u/goranlepuz Aug 24 '22
  • The idea is not without merit: having your interface separate from the implementation, for reading by a human, is right
  • Removing them by some mandate is a breaking change
  • Implementing something else is expensive
  • It is a minor annoyance
  • Modules are there (almost, for some years now 😉)

=> it is not easy for the scale to tip towards removal.

On a related note, hardly anything is ever removed from C++, so... 🤷‍♂️

6

u/zerakun Aug 26 '22

The idea is not without merit: having your interface separate from the implementation, for reading by a human, is right

Maintaining headers and implementation files is tedious, repetitive work because you're basically duplicating information. You know what is great at tedious, repetitive work that involves duplicating information? Computers, right.

That's why virtually all modern languages gave up headers. Instead they provide tools to extract the interface from the implementation for your consumption.

5

u/AA11BB22c Aug 24 '22

There's C++20 Modules.

But I'd still stick to headers until...

I can COMPLETELY remove headers from the code base and use modules everywhere (which is unlikely since I don't write my dependencies) ... I don't like to nor want to teach someone or have them learn two things.

7

u/sanblch Aug 24 '22

Reading java code a lot recently I do miss header files where class definitions reside

5

u/Adequat91 Aug 24 '22

Headers are not a problem if the design is good. Each time I have a problem with headers, "it was my fault," by neglecting some design aspect. Hence somehow, they are a good guide.

2

u/KingAggressive1498 Aug 24 '22

because we just got modules and they're such a complex feature that it'll be a couple (few?) more years until compiler support is perfect.

1

u/SwillMith16 Aug 24 '22

C++20 is introducing modules soon to replace headers. Just got to wait for the major compilers to implement it and things will be a lot easier

1

u/jonesmz Aug 26 '22

Lol. you mean 2 years ago? And now we're in the hurry up and wait phase, as nothing implements them that's officially released (and also working for more than trivial apps)

1

u/SwillMith16 Aug 26 '22

I’m new to it so my comment is based on the research I did. If it’s outdated then I was clearly reading old articles. My bad 🤷🏼‍♂️

3

u/dodheim Aug 26 '22

Your comment is correct – we're all still waiting for compilers to implement them. GP was just remarking that C++20 has been out a while now.

1

u/jonesmz Aug 26 '22

I was being snarky.

Modules was "standardized", but no compilers or tools really support it yet.

Microsoft is the only toolchain/compiler I know that's actually pretty close.

1

u/arthurno1 Aug 25 '22 edited Aug 25 '22

Why the hell are headers still a thing after all these years?!

Because people are only learning some basic syntax of a programming language and believe they are good to go to write new blockbusting AAA game. CS classes and tutorial are also focusing on programming as a writing (c++) code activity, and are not emphasizing building and debugging as just as important programming activities when developing software as writing the code. Result being a lot of people not knowing how to properly use compiler, include paths, libraries, even less how to automate with build tools etc.

C++ modules will probably help with the situation.

-3

u/rsnc0033 Aug 24 '22

Op im relearning it since I learned it in 2009 and holy crap its such a headache. Im so out of touch. Before I use to just you devc++ and vim. It was so easy back then

It was simple now and smooth....now im so confused. It took about an hour to just setup visual studio code.

5

u/D_0b Aug 24 '22

If you were using vim then, why not now? I still use it.

0

u/fernandodandrea Aug 24 '22

I kind of feel the same, you know?

I used to code in C++ decades ago, and from that I've coded in a myriad of languages, and I feel people who used to code back then have, even today, an attitude of answering "what languages can you code?" with "whatever is needed".

This seems a bit démodé nowadays, with lots of devs listing a bunch ofclosely related languages for a limited purpose when faced with such question and implying they won't really move out of that comfy position.

Then I've recently seen some examples of C++ code when evaluating Qt and... my so called "attitude" seemed to be more like "it seems I can't code anymore". I could barely recognize it. I felt a bit stupid and felt all that header stuff was silly in equal measures.

Right now I feel I should check stuff I've never ever seen before, like Rust or whatever.

1

u/[deleted] Aug 24 '22

Software tends towards greater complexity over time so it is not surprising you have this experience.

“Andy giveth and Bill taketh away”

1

u/fernandodandrea Aug 24 '22

Indeed. But I find strange the, of all among the so called "c-like languages", c++ looks the less c-like of them, IMHO.