473,372 Members | 819 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,372 software developers and data experts.

header files & #includes

Is it best to place all #include files in one header file seperate
from all the other files in a project?

What is the logic that determines the order of the #include files,
does it depend on the order of function calls in main?
Jul 22 '05 #1
7 2062

"header_question" <he****@file.net> wrote in message news:28********************************@4ax.com...
Is it best to place all #include files in one header file seperate
from all the other files in a project?
If a header file needs something in another header itself, then included it in the
header. Otherwise included things in the cpp files as they are needed. For
example. If your class definition in your own header needs vector, include it
there.
What is the logic that determines the order of the #include files,
does it depend on the order of function calls in main?


No, the only thing that matters is that things that declare things used in
later includes have to be included first. Again this is accomplished
easiest by including things as they are needed.
Jul 22 '05 #2
"header_question" <he****@file.net> wrote in message
news:28********************************@4ax.com...
Is it best to place all #include files in one header file seperate
from all the other files in a project?
It's not something I would do.

What is the logic that determines the order of the #include files,
does it depend on the order of function calls in main?


It does not depend on the order of function calls. If header file A includes
(or might include at some future date) header file B, then I think B should
go first. I usually put standard headers first, third party headers next,
and my own headers last. (Standard headers never depend on third party
headers, and third party headers never depend on my own headers.)

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
header_question wrote:
Is it best to place all #include files in one header file seperate
from all the other files in a project?

What is the logic that determines the order of the #include files,
In most environments, the headers can be included in any order, and
there is no advantage to including them all in one "master header."

In environments that support pre-compiled headers, there is often a
significant advantage to including headers in the same order within each
translation unit. In this case, the "master header" approach can be
useful. Unless you know that you are using such an environment, don't
bother complicating your file structure.
does it depend on the order of function calls in main?


No.

Jul 22 '05 #4
On Thu, 01 Jan 2004 20:11:42 +0000, header_question wrote:
Is it best to place all #include files in one header file seperate
from all the other files in a project?
Normally it is frowned upon. Include only what you need. The only
exception is when you use precompiled headers, in that case the
compile-speed can increase dramatically from placing all includes in a
single file. In practice I find this does not really work, as I more often
change include files than source files (thanks to templates), negating the
gain.
What is the logic that determines the order of the #include files,
does it depend on the order of function calls in main?


There should be no hard dependencies between include files, so
technically, include them in any order you want.

My eprsonal standard is
- First project include files
- Then own library include files
- Then 3rd party include files
- Last standard library include files

The reason is that this makes it easier to spot when an include file does
not include a required include file itself. Others disagree with this
though, it is mainly a style issue.

HTH,
M4

Jul 22 '05 #5
My personal preference is the reverse:
My eprsonal standard is
- First project include files
- Then own library include files
- Then 3rd party include files
- Last standard library include files


1.) standard library include files
2.) 3rd party include files
3.) my own include files
4.) the project include files

--The Directive
Jul 22 '05 #6
On Thu, 01 Jan 2004 18:44:56 -0800, The Directive wrote:
My personal preference is the reverse:
My eprsonal standard is
- First project include files
- Then own library include files
- Then 3rd party include files
- Last standard library include files


1.) standard library include files
2.) 3rd party include files
3.) my own include files
4.) the project include files


Any reason for that?

M4

Jul 22 '05 #7

"The Directive" <th***********@hotmail.com> wrote in message
news:84**************************@posting.google.c om...
My personal preference is the reverse:
My eprsonal standard is
- First project include files
- Then own library include files
- Then 3rd party include files
- Last standard library include files


1.) standard library include files
2.) 3rd party include files
3.) my own include files
4.) the project include files

--The Directive


Thanks to Lakos "Large Scale C++" for pointing out to me that regardless of
the order of the rest every .cpp should always
include its own .h file first.
The reason: If you do this then every compile checks that the .h file stands
on its own as it should (i.e. does not require other headers and or using
directives to be included before it).

eg. the most common problem is that you get X.cpp defining ostream&
operator<<(ostream&,X&) and not including
<iostream> or <iosfwd> or not fully qualifying the names such that it will
only compile if all headers use:
#include <iostream.h> // i.e. <iostream> using std::iostream;
#include "X.h"

If this strategy is followed consistently then it gaurantees (at least where
there is a cpp for each .h) that the order of the rest does not matter.
This is so useful that I have a policy of creating a trivial cpp file for
every .h even though all it contains is a #include of the header.

Having started with "X.h" aesthetics would tend to favour the order

FIRST "X.h"
- First project include files
- Then own library include files
- Then 3rd party include files
- Last standard library include files

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris Mantoulidis | last post by:
Seperate compilation (that's what it's called, right?) seems to be quite popular, so I decided to get some info about it, and (d'oh) use it... But it's whole structure seems weird to me... ...
16
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
15
by: Kannan Goundan | last post by:
Maintaining a C++ header file can be painful. I want a way to automatically generate header files from the implementation file. Does anybody know of a program that does this? If not, I'd like...
3
by: Code4u | last post by:
We have a mature project that suffers long build times because many modules pull in far more than they need. In the long term I would like to refactor to break some of the dependencies, but I would...
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...
15
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
16
by: wdh3rd | last post by:
Hi everyone. I'm new to C and I have a few questions: I am making files for permutations and combinations. Files to be made are perm.c, perm.h, combo.c, and combo.h. Since both combinations...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.