#include's in multiple modules

This is a discussion on #include's in multiple modules within the C Programming forums, part of the General Programming Boards category; I am working on a relatively big program that is divided in multiple modules, and I just want to know: ...

  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
    Katy, Texas
    Posts
    2,309
    The order or number of #includes make no difference towards "program efficiency". They are a compile-time thing, not a run-time thing.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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, 05:13 AM
  2. Multiple Modules
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 02-18-2006, 03: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, 01:54 PM
  5. multiple header #includes
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 12-30-2002, 09:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21