Thread: #include's in multiple modules

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    #include's in multiple modules

    I am working on a relatively big program that is divided in multiple modules, and I just want to know:

    Which is more program efficient? Writing a header file that has all the #include's the program needs, or #include'ing the necessary files for each module separately? The thing is that I am not sure if the compiler preprocesses every single module first and them compiles them all together, or if the compiler preprocesses the modules and compiles them seperately.

    Thanks.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The order or number of #includes make no difference towards "program efficiency". They are a compile-time thing, not a run-time thing.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    cool... so even If I include useless header files, the compiler will not use them if it doesn't need too?

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    That's for the linker to decide. The final step in compiling an executable or library.

    Even multiple inclusion compile-time performance issues are usually handled
    cleanly from the compiler, include-guards or #pragma once are probably just
    as efficient nowdays.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > or #include'ing the necessary files for each module separately?
    This would be my preference, because
    - it's obvious from looking at the code what the module actually depends on. #include "everything.h" tells you nothing, should you ever want to take your module and re-use it somewhere else.
    - a single catch-all include will necessarily have to include things which modules don't need, so there is an incremental cost in the compilation time of each module.
    - if you change just ONE header which everything.h includes, then you have to recompile the ENTIRE project.

    A consequence of only including the headers you need is that if forces you to think about what you're doing. The need to include a header might alert you to your interfaces and code structure being broken.

    > Writing a header file that has all the #include's the program needs,
    If you're lazy, and your compiler supports precompiled headers, then it may work for you.
    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.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    - a single catch-all include will necessarily have to include things which modules don't need, so there is an incremental cost in the compilation time of each module.
    That is, unless it's a precompiled header.

    Quote Originally Posted by Salem View Post
    > Writing a header file that has all the #include's the program needs,
    If you're lazy, and your compiler supports precompiled headers, then it may work for you.
    However, you should still beware, since changing a header which is included in a precompiled header still triggers this condition:

    - if you change just ONE header which everything.h includes, then you have to recompile the ENTIRE project.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks a lot for the explanation. I'll keep that in mind.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by samus250 View Post
    Which is more program efficient? Writing a header file that has all the #include's the program needs, or #include'ing the necessary files for each module separately?
    In general, a header file should include all the headers it requires internally (which basically boils down to data types which are referenced in the header file), and a source module should also include any headers it requires. It's not good to depend on one header pulling in another header, even if you know that it does, because that may change in the future.

    This can lead to "redundant" inclusion of headers, but it's harmless and far preferable to the alternative, where you have to guess which header might possibly pull in which other header.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding multiple includes
    By plan7 in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 06:13 AM
  2. Multiple Modules
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 02-18-2006, 04:34 PM
  3. deal with multiple includes
    By rockdj in forum C++ Programming
    Replies: 4
    Last Post: 07-29-2004, 09:07 AM
  4. prototypes and multiple modules
    By Bigbio2002 in forum C Programming
    Replies: 6
    Last Post: 11-19-2003, 02:54 PM
  5. multiple header #includes
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 12-30-2002, 10:04 PM