#### Archived

This topic is now archived and is closed to further replies.

# Header file order

## Recommended Posts

Hello, I was just wondering whether or not including all your header files into one header and then including that one header in all your files is efficient or not. Does it increase your executable size? Bloat the code? Or is it more efficient than having multiple #include's in one *.cpp file. Here is my example (it's in the stdafx.h header):
  #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Include necessary header files #include #include // Header file for the OpenGL32 library #include // Header file for the Glu32 library #include // Holds the file manipulation routines #include // Holds other routines (i.e. rand()) #include "GLTimer.h" // Holds a frames per second timer class #include "GLWindow.h" // Holds the information about the window #include "GLImage.h" // Contains the GLImage class #include "GLMain.h" // Holds the main entry point of the OpenGL application #include "GLLog.h" // Holds a class that outputs log files #include "resource.h" // Holds the resource ID's of various items extern GLLog eLog; // Prevents compiler errors - defined later (in stdafx.cpp) 
This makes it so each other header files doesn't need to have a #include in it, and any global variables (I try not to use them) I include in stdafx.cpp like the eLog. All my header files have:
  #ifndef CLASS_NAME #define CLASS_NAME class definition without any #include's ... ... #endif 
So, all I have to do in a .cpp file is:
  // Include neccessary header files #include "stdafx.h" // Header file for Windows .. do the functions etc. 
Is this an efficient way of linking your code together? Or should I just #include the necessary headers in the *.cpp files. Any suggestions would be greatly appreciated, EDIT: Source thing is acting strange... EDIT: Really strange... EDIT: I think I fixed it, the comments aren't aligned though EDIT: Another one... EDIT: Sorry about all the edits, the format just wasn't right Edited by - Viscous-Flow on November 9, 2001 4:31:43 PM Edited by - Viscous-Flow on November 9, 2001 4:32:51 PM Edited by - Viscous-Flow on November 9, 2001 4:34:22 PM Edited by - Viscous-Flow on November 9, 2001 4:35:18 PM Edited by - Viscous-Flow on November 9, 2001 4:36:13 PM Edited by - Viscous-Flow on November 9, 2001 4:36:56 PM Edited by - Viscous-Flow on November 9, 2001 4:37:59 PM

##### Share on other sites
The problem with adding non "standard library" includes (essentially your own headers) to your PCH header is that whenever you make a change to your own source or header files, those portions of the PCH have to be rebuilt (essentially negating the gain). Don''t put files in a PCH header just so you don''t have to include them everywhere else; put them there because they are extremely unlikely to change.

Also, including headers in a file that doesn''t need them lengthens the compilation time, because it takes longer to fully preprocess the file. The preprocessor actually substitutes the entire text of an included file in place of the #include directive before handing the file over to the compiler. In other words, unnecessary includes make the file larger and force the compiler to deal with more declarations and/or definitions that are unnecessary for the compilation of that file.

In conclusion, only put includes in the PCH header file that are required by *every* (or nearly every) source file in the project.

I wanna work for Microsoft!

##### Share on other sites
Ok, thanks Oluseyi.

• ### Forum Statistics

• Total Topics
628367
• Total Posts
2982278

• 10
• 9
• 13
• 24
• 11