[C++] Order of header inclusion

Discussion in 'C++' started by qazmlp, Aug 22, 2003.

  1. qazmlp

    qazmlp Guest

    I need to include a list of
    - C++ headers
    - headers of other modules
    - headers of my module
    - implementation specific ie.OS headers


    In what order, they should be included in my .CPP file? What is the
    best followed approach?
    qazmlp, Aug 22, 2003
    #1
    1. Advertising

  2. qazmlp

    Attila Feher Guest

    qazmlp wrote:
    > I need to include a list of
    > - C++ headers
    > - headers of other modules
    > - headers of my module
    > - implementation specific ie.OS headers
    >
    >
    > In what order, they should be included in my .CPP file? What is the
    > best followed approach?



    It is all just one opinion:

    It is a common rule to include first the standard headers.

    Then you include the header for the implementation (like for the class),
    which in turn includes all the headers needed for the declaration.

    Then you include any additional headers needed for the implementation.

    IIRC the only "rule" I have heard is to include the standard headers first.
    The cause for this rule is that if there is something "wrong" in your
    headers they cannot accidentaly modify the meaning of the standard headers.
    Of course, since the other headers might also include standard headers it
    can never really be done that all standard headers are included first.

    IMHO the order of inclusion is usually a matter of taste. Each header file
    in itself must "work" without any other header included before (meaning that
    it should include the needed headers, predeclare the needed incomplete types
    etc.) So as long as you have no "errors" it is rather a matter of personal
    taste and readibility. For example people usually do not mix those headers,
    so they first include the standard ones (usually C and C++ headers separated
    by a blank line), then (let's say) the OS headers, then the possibly the
    other headers needed by the implementation, then the implementation's own
    header. I know, it is a different order than I have said before - that is
    the reason why I have said that it is /mostly/ a matter of taste or personal
    style.

    --
    Attila aka WW
    Attila Feher, Aug 22, 2003
    #2
    1. Advertising

  3. "Attila Feher" <> wrote in message news:<bi4dtt$qtc$>...

    > It is all just one opinion:
    >
    > It is a common rule to include first the standard headers.
    >
    > Then you include the header for the implementation (like for the class),
    > which in turn includes all the headers needed for the declaration.
    >
    > Then you include any additional headers needed for the implementation.


    I do the opposite: I include the primary header (with declarations to
    be defined in the .cpp file), then other project headers, then system
    headers (anything outside my project) in an organized but admittedly
    arbitrary order.

    With my way, I know if I forgot to include another header in one of
    mine because mine is included first. With yours, my header may be
    included after other headers it needs, thus making them available even
    though mine didn't include them itself.

    > IIRC the only "rule" I have heard is to include the standard headers first.
    > The cause for this rule is that if there is something "wrong" in your
    > headers they cannot accidentaly modify the meaning of the standard headers.


    How, beyond macros, can you modify a header by defining things
    yourself first? If that's the only way, you'd almost have to go out of
    your way to do such a thing.

    > IMHO the order of inclusion is usually a matter of taste.


    Absolutely correct for well-written headers. Defending against other
    people's poorly-written headers is better done on a case-by-case basis
    than by rule of thumb. On the other hand, defending against your own
    headers can best be done using my order of inclusion (IMO).

    - Shane
    Shane Beasley, Aug 22, 2003
    #3
  4. qazmlp

    Attila Feher Guest

    Shane Beasley wrote:
    [SNIP]
    > With my way, I know if I forgot to include another header in one of
    > mine because mine is included first. With yours, my header may be
    > included after other headers it needs, thus making them available even
    > though mine didn't include them itself.


    Correction: with the practice I have mentioned. I have never told it is
    mine. But since it has been presented as a common coding rule to me on many
    occasions (ACCU conference was one of them) I thought I introduce it here.

    > How, beyond macros, can you modify a header by defining things
    > yourself first? If that's the only way, you'd almost have to go out of
    > your way to do such a thing.


    I have never done such a thing. That was what has been named as a reason
    for the coding rule.

    >> IMHO the order of inclusion is usually a matter of taste.

    >
    > Absolutely correct for well-written headers.


    That is what I have said. :)

    > Defending against other
    > people's poorly-written headers is
    > better done on a case-by-case basis
    > than by rule of thumb.


    The idea is not protecting against them but to make finding them simple.
    Meaning that if there is a 3rd party product and you include its headers you
    may not be able to diff all the headers to figure out that one of them does
    something wrong after the update. So this header inclusion order thing is
    for ease of finding mistakes in (I guess) 3rd party code, not for finding
    mistakes in the code we are in charge of (and have tested, unit tested, code
    reviewed etc.).

    To be honest as I work at the workplace is to follow the existing
    practices - if they suffice. :)

    > On the other hand, defending against your own
    > headers can best be done using my order of inclusion (IMO).


    I agree. Defending against missing standard headers (and error I have seen)
    is a bit better, but since there might be other (your) headers before that
    including the right std (or system) header or one which includes it... this
    is not a foolproof test either. That is why I dared to say: it is rather a
    matter of taste. But I am open to hear if anyone has a good argument for a
    certain inclusion order.

    --
    Attila aka WW
    Attila Feher, Aug 22, 2003
    #4
  5. qazmlp

    Greg C Guest

    (qazmlp) wrote in message news:<>...
    > I need to include a list of
    > - C++ headers
    > - headers of other modules
    > - headers of my module
    > - implementation specific ie.OS headers
    >
    >
    > In what order, they should be included in my .CPP file? What is the
    > best followed approach?


    Switch to a language that doesn't use header files?
    Greg C, Aug 22, 2003
    #5
  6. qazmlp

    Andre Kostur Guest

    "Cagdas Ozgenc" <> wrote in
    news:bi51hh$98$:

    > One of the stupid things of file inclusion is that you cannot keep
    > track of the dependencies. For example some of my source files compile
    > fine on some compilers but not on other due to my neglegance in
    > including the appropriate headers. In one compiler the required
    > dependency comes through some other header by luck, and in the other
    > it doesn't.
    >
    > I haven't yet managed to find a good solution to that other than
    > compiling my source on various compilers (and accompanying standard
    > library implementations).


    The good solution is to always include the headers needed for your uses,
    and to not assume that one header will include another, unless it is
    explicitly documented (ie: in the Standard, not in the header files) that
    it will do so.
    Andre Kostur, Aug 22, 2003
    #6
  7. qazmlp

    White Wolf Guest

    Greg C wrote:
    [SNIP]
    >> In what order, they should be included in my .CPP file? What is the
    >> best followed approach?

    >
    > Switch to a language that doesn't use header files?


    First please switch newsgroup.

    --
    WW aka Attila
    White Wolf, Aug 22, 2003
    #7
  8. qazmlp wrote:

    > I need to include a list of
    > - C++ headers
    > - headers of other modules
    > - headers of my module
    > - implementation specific ie.OS headers
    >
    >
    > In what order, they should be included in my .CPP file?
    > What is the best followed approach?


    It doesn't matter which order you include header files
    if they are both self sufficient and idempotent.


    "Public header files should be

    1. self sufficient and
    2. idempotent.

    Self sufficient means that
    no other header files or definitions need to be included
    before you include the public header file.
    Idempotent means that
    the content of the public header file is included no more than once
    regardless of how many times it is included by other header files.

    Nor should public header files include any statements
    that cause the compiler to generate code or allocate memory."
    E. Robert Tisdale, Aug 22, 2003
    #8
  9. qazmlp wrote:

    > I need to include a list of
    > - C++ headers
    > - headers of other modules
    > - headers of my module
    > - implementation specific ie.OS headers
    >
    >
    > In what order, they should be included in my .CPP file? What is the
    > best followed approach?


    I like to include the header for this .cpp file first. This way, the
    compiler will complain if my header file is missing some forward
    declarations or header files. Also, it means that if other people use my
    header file, it is guaranteed to work no matter which order they include
    the file.

    The rest is random, but I like to group them together, and the C++ headers
    come first.

    --
    John L. Fjellstad

    A: Top posting!
    Q: What is the most irritating thing on Usenet?
    John L Fjellstad, Aug 22, 2003
    #9
  10. qazmlp

    Andre Kostur Guest

    "John Harrison" <> wrote in
    news:bi7q5m$61k9k$-berlin.de:

    >
    > "Andre Kostur" <> wrote in message
    > news:Xns93DF5C6448F2Fnntpspamkosturnet@209.53.75.21...
    >> "Cagdas Ozgenc" <> wrote in
    >> news:bi51hh$98$:
    >> The good solution is to always include the headers needed for your
    >> uses, and to not assume that one header will include another, unless
    >> it is explicitly documented (ie: in the Standard, not in the header
    >> files) that it will do so.

    >
    > Its not easy to keep track of which headers your source files need.
    > When one standard header includes another (in a non-standard way) its
    > very easy to miss the dependency your code has on that header.


    Well.... then one should be adding the header for any new stuff that you
    use (ie: I just added an std::eek:stringstream in my program, I should go add
    #include <sstream> to my list of headers....). As opposed to adding new
    stuff, then adding headers until it compiles.... (Now, granted, I'm guilty
    of this method too.... but in a _perfect_ world :) )
    Andre Kostur, Aug 25, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Burgess

    C++ Header Inclusion

    Matthew Burgess, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    2,076
    Mike Smith
    Aug 28, 2003
  2. Johannes Bauer

    Header inclusion question

    Johannes Bauer, Dec 7, 2004, in forum: C++
    Replies:
    6
    Views:
    417
    EventHelix.com
    Dec 8, 2004
  3. qazmlp

    Header Inclusion style

    qazmlp, Jul 8, 2003, in forum: C Programming
    Replies:
    14
    Views:
    539
    Casper H.S. Dik
    Jul 10, 2003
  4. Johannes Bauer

    Include guards and inclusion order

    Johannes Bauer, Dec 1, 2007, in forum: C++
    Replies:
    18
    Views:
    630
    James Kanze
    Dec 3, 2007
  5. Pavel
    Replies:
    3
    Views:
    264
    Pavel
    Jul 9, 2009
Loading...

Share This Page