3

Remembering the names of system header files is a pain...

Is there a way to include all existing header files at once?

Why doesn't anyone do that?

Some Noob Student
  • 14,186
  • 13
  • 65
  • 103
  • 1
    If it's such a pain to remember, you could use eclipse and its "add include" feature. – Nate Nov 12 '11 at 00:47
  • Of course "there is a way". But it's not a good idea. Perhaps the more experienced C programmers simply *know* where their dependencies are declared and include only what they need with full consciousness. – Kerrek SB Nov 12 '11 at 00:47
  • 3
    If this was a good idea - there would only be 1 header file for the entire language – Adrian Cornish Nov 12 '11 at 00:45
  • 1
    I think it's an incredibly bad idea! – ldav1s Nov 12 '11 at 00:48
  • @AdrianCornish IIRC, a very early pre-STL C++ standard library draft had just that ... the header that included all the C++ headers. Then in a fit of sanity, they got rid of it ... and the rest of the draft and started over with STL. – ldav1s Nov 12 '11 at 00:58
  • Sarcasm doesn't belong in an answer. Answers are for just that - answers to questions. – Ken White Nov 12 '11 at 01:01
  • @Nate, I never said there should be no humor or sarcasm in an answer. I said humor or sarcasm should not *constitute* the answer with no other content. I actually have what my friends and acquaintances call a good sense of humor (so they say, anyway). Humor/sarcasm with nothing else should be made as comments, not posted as answers to questions. This site is good in part because there's not a lot of noise and clutter in the questions and answers; I'm in favor of keeping it that way. :) I didn't downvote anyone, if you'll notice - I gave ldav1s the opportunity to delete the answer instead. – Ken White Nov 12 '11 at 01:48

6 Answers6

12

Including unneeded header files is a very bad practice. The issue of slowing down compilation might or might not matter; the bigger issue is that it hides dependencies. The set of header files you include in a source file should is the documentation of what functionality the module depends upon, and unlike external documentation or comments, it is automatically checked for completeness by the compiler (failing to include needed header files will result in an error). Ensuring the absence of unwanted dependencies not only improves portability; it also helps you track down unneeded and potentially dangerous interactions, for instance cases where a module which should be purely computational or purely data structure management is accessing the filesystem.

These principles apply whether the headers are standard system headers or headers for modules within your own program or third-party libraries.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
5

Your source code files are preprocessed before the compiler looks at them, and the #include statement is one of the directives that the preprocessor uses. When being preprocessed, #include statements are replaced with the entire contents of the file being included. The result of including all of the system files would be very large source files that the compiler then needs to work through, which will cost a lot of time during compilation.

David Alber
  • 17,624
  • 6
  • 65
  • 71
5

No one includes all the header files. There are too many, and a few of them are mutually exclusive with other files (like ncurses.h and curses.h).

It really is not that bad when writing a program even from scratch. A few are quite easy to remember: stdio.h for any FILE stuff; ctype.h for any character classification, alloc.h for any use of malloc(), etc.

If you don't remember one:

  • leave the #include out
  • compile
  • examine first few error messages for indication of a missing header file, such as some type not declared, or calling a function with assumed parameter types
  • figure out which function call is the cause
  • look at the man page (or whatever documentation your compiler has) for that function
  • notice the #include shown by the documentation and add it
  • repeat until all errors fixed

It is quite a bit easier for adding to an existing code base. You could go hundreds or thousands of working hours and never have to add a #include.

wallyk
  • 56,922
  • 16
  • 83
  • 148
2

No it is a terrible idea and will massively increase your compile times and possible make your exe a lot larger by including massive amounts of unused code.

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
1

I know what you're talking about, but I need to double-check the function prototypes for the functions I'm using (for ones I don't use daily, anyway) -- I'll just copy and paste the #includes straight out of the manpage for the associated functions. I'm already looking at the manpage (it's a simple K in vim(1)), so it doesn't feel like an extra burden.

sarnold
  • 102,305
  • 22
  • 181
  • 238
1

You can create a "master" header, where you put all your includes into. Then in everything else include it! Beware of conflicting definitions and circular references... So.... Master1.h, master2.h, ...

Not advocating it. Just saying.

Thomas Eding
  • 35,312
  • 13
  • 75
  • 106