473,547 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 6421
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**********@l mf.ericsson.se> wrote in message news:<bi******* ***@newstree.wi se.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********@redi ffmail.com (qazmlp) wrote in message news:<db******* *************** ****@posting.go ogle.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.co rnell.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
1737
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 dependencies of these dependencies and so on. This is quite ugly. A start of an "Example.cpp" file could look like this
18
2226
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 the implementation of its member functions in foo.cpp (which obviously #inludes foo.h) and further assume that it depends on a class bar which is...
60
8220
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 associated with this source file For example, in a file hello.c: #include <stdio.h>
18
2724
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 (collection of files) which are not required, except for one header file's contents. I'd say 'No, header files should be included in the C source,...
8
3037
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 structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h ->...
6
5185
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
3030
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 at the beginning since compiler is not complaining about this. But I recently add another structure, it starts to complain. give me warnings like...
3
2020
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. (code in source-code file declaration in header file, as usual) 2. Write a central header file which first holds forward declarations of all classes...
4
2029
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 main.c. Now I create a header file which represents the interface of functions.c to my main program file main.c. I put in the header file: all function...
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.