3

Possible Duplicate:
In C++ why have header files and cpp files?

Coming from a C# background, I find header files really annoying. Are they necessary even with C++11?

Community
  • 1
  • 1
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • 2
    @CodingMash If the standards committee would have found a good way for modules there might have been a replacement. But that's a quite complex topic for doing right ... – johannes Oct 06 '12 at 12:47

3 Answers3

2

C++ needs header files (and libs too) just like C# needs reference assemblies.

C.J.
  • 15,637
  • 9
  • 61
  • 77
1

Yes, because it's still based on C. You can answer your own question: Don't use them and try to compile without them. If you can't, then the compilers still require them.

I don't mean that as a tautology. If the language spec still calls for them, and the compilers match the spec, then it'd be a massive effort to change. And the debate would take a very long time. This sounds like a question that is meant for little more than venting and debate with no real end.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • One can write and compile full applications without using headers. There's nothing in the standard forcing you to use any header. (of course this won't be real life code, only for very simple tests etc) ;-) – johannes Oct 06 '12 at 12:45
  • @johannes the C++ standard defines headers for the standard library. Nothing ofrces you to use them, but if you want to use the standard library, you have to use these headers. – juanchopanza Oct 06 '12 at 12:52
  • @juanchopanza Yes, still I can write trivial test programs without those. nothing else I said (and well, some compilers might use some form of "built-in headers" for standard headers, those might not be "header files" but now we really become philosophical, I just wanted to comment on this answer which suggest you can't compile without headers, there are many things you can't do without including those, but there are still headerless-options left) – johannes Oct 06 '12 at 13:03
  • Can you write non-trivial programs without them? "Hello world" doesn't prove anything. – duffymo Oct 06 '12 at 13:12
  • @johannes sure, I agree that you can do a lot without headers. But templates are such a big part of C++ and the standard library, and I cannot think of an easy way to provide templates to client code without header or include files. – juanchopanza Oct 06 '12 at 13:16
  • there has to be a better way... writing header file is just stupid. – MartianMartian Jun 08 '19 at 14:56
  • C was invented with Unix. It’s not changing regardless of your opinion. – duffymo Jun 08 '19 at 15:10
0

Yes they are. Assume you want to develop a DLL or SO. How should a developer know which functions this library offers? He needs therefore prototypes called API to include into custom projects. This way one splits up declaration and implementation. You declare which functions are available but you can always change the internal code.

Guilherme Bernal
  • 8,183
  • 25
  • 43
Christian Ivicevic
  • 10,071
  • 7
  • 39
  • 74
  • Given what you said, what if we suppose the OP ins't talking about a DLL/SO, but rather just an end program, which also undergoes frequent updates and refactoring. Does the compiler enforce the header files at all cost and one MUST be editing 2 files every time a signature changes/member is added? – FMA Mar 13 '18 at 16:56