473,908 Members | 4,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2100

"header_questio n" <he****@file.ne t> wrote in message news:28******** *************** *********@4ax.c om...
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_questio n" <he****@file.ne t> wrote in message
news:28******** *************** *********@4ax.c om...
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.goo gle.com...
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<<(ostr eam&,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
2237
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... Here's what I think of how it is (from what I've read): THE PROJECT +1st header file
16
12556
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 because it is, as the authors love to say, "implementation specific". If that's the case, how does the compiler commonly handle them? I use Linux and gcc specifically. Basically, I don't understand how a header file being included makes a...
11
2769
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 understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
15
7942
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 to try writing one. The input file would consist of function definitions and class definitions (with all methods defined inline). The program would extract all the signatures and place them in a header file. I can see some small problems...
3
4066
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 like to start by eliminating unneeded header includes. Is there a tool that can parse a set of source files and identify redundancies?
60
8341
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
2771
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, not in another
15
4424
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 that will let us figure out which header files are actually needed by each .cpp, and allow us to break this up so that we're not including the world in each one. Ideally, the same tool would also recognize where #includes can be replaced with...
16
2096
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 and permutations use factorial to solve their problems, factorial is supposed to be in both perm.c and combo.c, and declared private. (1) You wouldn't ever put a prototype in a header if that function was
0
9875
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11335
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11040
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9721
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8094
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7244
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5930
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3354
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.