473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

[C++] Order of header inclusion

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
8 6411
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
"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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Johannes Bauer | last post by:
Hi group, I've got a question concerning inclusion of .hpp files. Currently I'm including all needed header files in the .cpp file. This means all dependencies of the package and all...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
8
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a...
6
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
7
by: questions? | last post by:
I have a file including all my function prototypes. I include them at the most top of my program. But some of the prototypes include the structures I declared later. I didn't notice the problem...
3
by: Henning Hasemann | last post by:
Hi all, On larger projects I used the following 'technique' (well in fact its a really simple thing): 1. Write one source-code file for each 'large' class and one corresponding header file....
4
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.