2

Possible Duplicate:
Coding C++ without headers, best practices?

Inspired by this question - the OP just finds the concept of headers confusing and depressing there, it's his subjective experience.

Now are there any objective reasons and scenarios for avoiding headers when coding in C or C++?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 3
    What's wrong with the answers at the other question? – David Heffernan Mar 16 '11 at 08:29
  • 3
    @David Heffernan: They answer "how to do that" and I'm asking "why would one do that"? – sharptooth Mar 16 '11 at 08:31
  • @David, the answers in the other question address whether it's okay (or not) to do this, rather than - what possible reason could exist for one to take this approach - so, IMO this is a valid question - but I certainly can't think of any...!! – Nim Mar 16 '11 at 08:32
  • 4 vote closes for 3 different reasons. – CashCow Mar 16 '11 at 09:54

6 Answers6

5

There are no objective reasons, only irrational ones borne of subjective bias induced by experiences with other languages for which headers don't exist as a concept.

The compilation unit is a fundamental concept in C and C++ compilers. A compilation unit must have access to all declarations at the source code level in order to access functionality provided by other compilation units, and the only logically coherent way to ensure that publishers and consumers of functionality see the declarations is for them to share the source code for those declarations. Headers are the natural way to achieve this.

Being a bit more technical about it, headers are not a C (or C++) concept, per se. They are preprocessor concept. When processing a source file, whenever the preprocessor encounters a #include directive, it replaces the directive with the entire contents of the header being referred to, recursively expanding any #include directives in the header itself. By the time the compiler proper kicks in, it sees nothing but a single "file" containing all the declarations it needs to do its job.

The key point is that, for better or worse, headers are the standard way to organise complex C and C++ code bases. You can play fun and games with #ifdefs to put both "header" and implementation in one source file, but that works against every tool-chain in the universe, and means that preprocessors have far more work to do, thus dramatically slowing down your builds.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • than why C++ committee is planing to add modular compilation feature to C++? – UmmaGumma Mar 16 '11 at 08:58
  • @Ashot: I am not suggesting that this is the way the world ought to be; I've been programming C and C++ for years and I am well acquainted with the pitfalls of using header files. I'm just stating how it should be done *given the current toolsets*. A modular compilation system for C++ would be a very interesting development. I haven't heard of this, though. Do you have a link? – Marcelo Cantos Mar 16 '11 at 09:01
  • see my answer there is a link. – UmmaGumma Mar 16 '11 at 09:02
  • @Ashot: I just had a quick skim. The first thing that popped to light was that headers are unavoidable for macros. Since macros are either directly or indirectly used by maybe 80% of header files ever written, this seems like a show-stopper to me. Perhaps there's meat there than I suspect, but I would be surprised if that proposal went anywhere soon. – Marcelo Cantos Mar 16 '11 at 09:16
0

None at all, it's just a bad idea.

The only objective reason would be if your C compiler could only include an extremely limited number of files over the course of a compilation, or the system you are working on having an extremely limited number of inodes.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • 1
    I guess then I would want to just have the number of all files as low as possible, not just header files, wouldn't I? – sharptooth Mar 16 '11 at 08:35
  • On such a system you would probably only be able to write small programs as well, so it wouldn't be that hard a limit. – Bo Persson Mar 16 '11 at 08:49
0

#includeing a header file tells the compiler, quite literally, "copy-paste some text here." Now perhaps your question is really asking, "are there any misuses for header files?" (and there certainly are), but as written, it makes as little sense as, "are there any objective reasons to avoid source files?"

user168715
  • 5,469
  • 1
  • 31
  • 42
0

I found header files great before I worked with java. Now I find them annoying, because it is code duplication, you have to keep 2 files in sync.

Of course, if you have a tool which builds the header dynamically, it's not that bad. Are there such tools?

user unknown
  • 35,537
  • 11
  • 75
  • 121
  • The code duplication is also a safety feature. If you type the function declaration twice, and they are equal, you have probably got it right. – Bo Persson Mar 16 '11 at 08:48
  • 2
    @Bo Persson: No, not really. Code duplication is *bad*, header files included. Code duplication is never safe, this just happens to be a code duplication error that's spotted by compiler. – Puppy Mar 16 '11 at 08:50
  • @DeadMG: Right, errors spotted by the compiler is good. :-) What if the header has the correct interface, and your implementation is wrong? – Bo Persson Mar 16 '11 at 08:52
  • If you don't do the duplication with copy and paste, it is serious work to do. If you do it that way, you don't catch such errors. – user unknown Mar 16 '11 at 08:55
  • You can write all your code in-line in the header file, only one file to maintain then. You then only need one .cpp file with your `main()` function. header files then become _source_ files You've still to `#include` your source files in other source files, but look Ma! No header files! (yes this is ridiculous, no I'm not being serious, yes I am attempting [Humor](http://en.wikipedia.org/wiki/Humour)) :) – Binary Worrier Mar 16 '11 at 08:57
  • It's even worse than that in C++, since the definition often *has* to look different to the declaration, due to member-function syntax, class and namespace scope, default arguments, and probably more. – Marcelo Cantos Mar 16 '11 at 08:59
  • @Binary Worrier: Yeah, I don't encourage misusing headers, just, that there are things, which can be annoying. – user unknown Mar 16 '11 at 09:18
0

No, header files are an essential tool and cannot be removed in the language as it is. That doesn't mean that they don't completely suck and shouldn't be removed ASAP.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

I have experienced one good reason for avoiding headers in C: distributing an open source application as a single .c file.

This is the distribution mode of dcraw, an ANSI-C cross-platform software for converting digital camera RAW files.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • That's not exactly a reason. That's just what they do. The question is why do they do it. I see no advantage of using a single file over multiple files as they will undoubtedly be wrapped inside a gz or zip file for transport. – Martin York Mar 16 '11 at 09:40
  • @Martin - As a user of that code, I find it easier to integrate to my own development project. gz or zip is one step more than raw source. – mouviciel Mar 16 '11 at 10:07