Thread: Headers

  1. #1
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127

    Headers

    I was just wondering something about header files. If I include a lot of unneccassary headers will it make the size of the executable bigger or will the linker only take the stuff that it needs? My point is that I have a header file with my functions in it and I alway forget to include those other headers that I need. For now I just have the includes in the header so that whenever I include my header I also include stdlib and math and sys/stat and stuff. Is this bad?
    ~Sven

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It would be pretty unusual for a header file to increase the size of the executable.

    The only real downside is the increase in compile times as the compiler has to read information it will never use.

    > I also include stdlib and math and sys/stat and stuff. Is this bad?
    It's all grey
    The "good" side is that if a header file #includes all it's dependents within it, then you as a programmer only has to #include "foo.h" to use foo, and you don't have to worry about what else it needs. From this point of view, it's good for the programmer.

    The "bad" side is say porting the code to another platform. The maintainer sees #include "foo.h", but has no idea what sort of dependencies there are on the rest of the system. Massive nesting of header files in a large system can make this tedious to work with.

    Personally, I'd say it's OK to include the C-Standard and POSIX header files directly in your own header files, but if one of your header files depends on another of your header files, then perhaps it's best to include them both in the source directly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Salem
    The "bad" side is say porting the code to another platform. The maintainer sees #include "foo.h", but has no idea what sort of dependencies there are on the rest of the system. Massive nesting of header files in a large system can make this tedious to work with.
    Which is why we don't #include <malloc.h> right?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    we don't include malloc.h because it isn't a standard header file.
    malloc is in stdlib.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM