Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ One header to rule them all


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 I put on my robe and...   Members   -  Reputation: 105

Like
0Likes
Like

Posted 29 November 2011 - 11:41 AM

My question involves header files in C++.

I currently have a few different classes and I separate them into a .h and a .cpp (such as regexClass.h and regexClass.cpp)

I have one header called theHeader.h and it includes all of my other headers:


#pragma once   // or I use ifndefs as well here
#include <iostream>
#include <fstream>
#include <string>

#include "regexClass.h"
#include "classificationClass.h"

enum fileType {

    ZIP = 0x4034b50,
    GZIP = 0x88b1f,
    SEVENZIP = 0xafbc7a37,
    BZIP = 0x39685A42,
};



when I build classifcationClass.cpp it requires access to the regexClass.h...... Instead of including regexClass.h, I just include theHeader.h.

#include "theHeader.h"
using namespace std;

classificationClass::classificationClass()
{
      init();
}

classificationClass::init()
{
 	regexClass *theRegex = new regexClass(); //I can create a regexClass without having to "#include "regexClass.h" up at the top, since it is already included in "theHeader.h"
 	
 	if(theRegex.match(ZIP, filePath))
   	cout<<"Victory"<<endl;
}


This grants classificationClass.cpp access to regexClass, <iostream>, <string>, etc. without me having to re-include everything all over again. Since the compiler will only include the files once, this code would work and compile just fine. The other crucial element to this working, is the order of file inclusion. classificationClass uses a regexClass object, thus "regexClass.h" is included in "theHeader.h" before "classificationClass.h" is.

My coworker stresses to me that this is a bad programming practice, and wants to copy my "theHeader.h" includes into each of my separate class .h files. Is it bad practice to do this? Or am I just that awesome?

Thanks :)

Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6991

Like
0Likes
Like

Posted 29 November 2011 - 11:53 AM

It is generally considered a bad idea. It will increase compile times a ton on large projects. Plus it doesn't clearly inform you of class dependencies, something you should always be aware of. Changing one header file will cause all the source files to rebuild, rather than just the source files that directly depend on that header.

Looking at your main header, it is not clear the order of the headers is significant. Just including regexClass.h in classificationClass.h makes the annoying 'order of inclusion' go away.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 swiftcoder   Senior Moderators   -  Reputation: 10367

Like
0Likes
Like

Posted 29 November 2011 - 12:03 PM

It is generally considered a bad idea. It will increase compile times a ton on large projects. Plus it doesn't clearly inform you of class dependencies, something you should always be aware of. Changing one header file will cause all the source files to rebuild, rather than just the source files that directly depend on that header.

What he said^.

However, it not unreasonable to place all your dependency inclusions (includes from system/3rd-party libraries you depend on) in a pre-compiled header file - this can (in some cases) significantly speed up compilation.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#4 iMalc   Crossbones+   -  Reputation: 2314

Like
0Likes
Like

Posted 29 November 2011 - 12:23 PM

Most of us know about the #include "everything.h" trick, and most of us don't use it.

Does that tell you something?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

#5 Serapth   Crossbones+   -  Reputation: 5755

Like
0Likes
Like

Posted 29 November 2011 - 12:23 PM


It is generally considered a bad idea. It will increase compile times a ton on large projects. Plus it doesn't clearly inform you of class dependencies, something you should always be aware of. Changing one header file will cause all the source files to rebuild, rather than just the source files that directly depend on that header.

What he said^.

However, it not unreasonable to place all your dependency inclusions (includes from system/3rd-party libraries you depend on) in a pre-compiled header file - this can (in some cases) significantly speed up compilation.


Yeah, this.

Frankly you are re-inventing stdafx, without any of the benefits. Look into pre-compiled header support. Generally you include large unchanging! headers in a single include, but others always include the minimum amount necessary. Where the argument gets really interesting is forward declare vs including.

#6 RobTheBloke   Crossbones+   -  Reputation: 2341

Like
0Likes
Like

Posted 29 November 2011 - 12:24 PM

It is generally considered a bad idea.


Personally, I'd replace 'generally considered' with 'always'.



Since the compiler will only include the files once, this code would work and compile just fine.


No. It will include the files ONCE within a compilation unit, if, and only if, #pragma once is supported by the compiler (just because sentry guards prevent the source being included twice does not mean the compiler magically avoids opening & reading the contents of the header file). Since a typical project may have a few dozen to a many tens of thousands of compilation units, you will be including far more than you need, and build times will rapidly spiral out of control (this is especially true for template heavy code).


Now, the oft used retort is "But it's only a small project, there's no need for me to worry about this!". Well, that's an invalid argument. There is always one certainty within any software project: Tomorrow the codebase will be larger than it is today. If you are sloppy with your includes today, you will have an enormous refactoring task looming in the distance. The only question you need to be asking is when are you going to do the refactor? (hint: It's easier if you do it now!)


My coworker stresses to me that this is a bad programming practice, and wants to copy my "theHeader.h" includes into each of my separate class .h files. Is it bad practice to do this? Or am I just that awesome?




It is an awful habit, and it's something you should unlearn as soon as possible. The good practice rules are this:


For header files:
* Minimise the number of includes to the absolute bare minimum (i.e. for base classes and member variable types only).
* Forward declare classes as much as possible to reduce the number of includes.
* To solve extreme build time problems, consider using the pimpl idiom.


For source files:
* Include the class header
* Only include additional headers as and when they are needed.



#7 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 29 November 2011 - 12:44 PM


It is generally considered a bad idea.


Personally, I'd replace 'generally considered' with 'always'.

There was one guy who used to hang around here, I forget his handle, who actually advocated this, as long as the master header was a pre-compiled header. He said that it actually improved compile times for large projects even if the pch had to be rebuilt every time anything was modified. I don't agree with him, but there are people out there who think otherwise.

Going back to disadvantages, keep in mind that a master header's effects may extend beyond just compile times. Every compilation unit will generate it's own copy of any global variables with internal linkage or inside anonymous namespaces as well as inline function definitions and potentially language support structures such as virtual function tables. Some linkers can merge/discard duplicate copies of some of these, but some cannot and other variables cannot be discard because the language prevents it. One particular case is global variables with creation side-effects designed to work around the static initialization order problems like the Zerob counter.

Then there are the inevitable problems with identifier pollution, such as the ever so fun preprocessor definition issues that can arise if you force every single file in your project to include windows.h. Some of the preprocessor changes are well known and easy to avoid, like the regular all-caps identifiers. Some regularly bite even people who know about them like min and max. Others are just obscure out of the blue PITAs like rad0.

#8 I put on my robe and...   Members   -  Reputation: 105

Like
1Likes
Like

Posted 29 November 2011 - 01:17 PM

Thank you very much for all of your responses! I knew this was the place to go to. I really appreciate it, and I will avoid using the "include everything" header from now on.

There was one guy who used to hang around here, I forget his handle, who actually advocated this, as long as the master header was a pre-compiled header. He said that it actually improved compile times for large projects even if the pch had to be rebuilt every time anything was modified. I don't agree with him, but there are people out there who think otherwise.

Going back to disadvantages, keep in mind that a master header's effects may extend beyond just compile times. Every compilation unit will generate it's own copy of any global variables with internal linkage or inside anonymous namespaces as well as inline function definitions and potentially language support structures such as virtual function tables. Some linkers can merge/discard duplicate copies of some of these, but some cannot and other variables cannot be discard because the language prevents it. One particular case is global variables with creation side-effects designed to work around the static initialization order problems like the Zerob counter.

Then there are the inevitable problems with identifier pollution, such as the ever so fun preprocessor definition issues that can arise if you force every single file in your project to include windows.h. Some of the preprocessor changes are well known and easy to avoid, like the regular all-caps identifiers. Some regularly bite even people who know about them like min and max. Others are just obscure out of the blue PITAs like rad0.


I especially appreciate this response. I was looking for a concrete reason as to why this would be a bad habit, but have been unable to find one. Thank you. I will go and eat crow now!

It is an awful habit, and it's something you should unlearn as soon as possible. The good practice rules are this:

For header files:
* Minimise the number of includes to the absolute bare minimum (i.e. for base classes and member variable types only).
* Forward declare classes as much as possible to reduce the number of includes.
* To solve extreme build time problems, consider using the pimpl idiom.
For source files:
* Include the class header
* Only include additional headers as and when they are needed.


Thank you for providing me with appropriate practice rules as well. Everybody's experience is much appreciated.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS