By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 980 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

[C++] Order of header inclusion

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Jul 19 '05 #2

P: n/a
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message news:<bi**********@newstree.wise.edt.ericsson.se>. ..
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
Jul 19 '05 #3

P: n/a
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
Jul 19 '05 #4

P: n/a
qa********@rediffmail.com (qazmlp) wrote in message news:<db**************************@posting.google. com>...
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?
Jul 19 '05 #5

P: n/a
"Cagdas Ozgenc" <co**@removeme.cornell.edu> wrote in
news:bi*********@news01.cit.cornell.edu:
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.
Jul 19 '05 #6

P: n/a
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
Jul 19 '05 #7

P: n/a
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."
Jul 19 '05 #8

P: n/a
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?
Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.