Header Files

Started by
4 comments, last by sdoherty55 22 years, 2 months ago
Has anyone ever found themselves including files such as everwhere in order to get your code to compile. This is probably a bit of opionion question, but where is the proper place to include your files. Assume that every class consists of a separeate .cpp and .h file. Where is the proper place to put you include files? Should they all go in the header, the source? What is the proper use of pre-compiled headers? Should pre-compiled headers be set to automatic? Opinions? Thanks
Advertisement
I had the same thing today. Was adding windows.h and gl/gl.h to half of my files. I decided to do something "different".

I created a file called "globals.h". Every CPP and H in my porject includes this and just this file. In that file I have (currently):

          #ifndef GLOBALS_H#define GLOBALS_H#include <windows.h>#include <gl\gl.h>#include <gl\glu.h>#include <gl\glaux.h>#include <math.h>#include <time.h>#include <stdio.h>#include "winmain.h"#include "resource.h"#include "ResourceIO.h"#include "GLFont.h"#include "ball.h"#include "player.h"#pragma comment( lib, "opengl32.lib" )#pragma comment( lib, "glu32.lib" )#pragma comment( lib, "glaux.lib" )#define PI 3.141592#endif          


This is no doubt a bad idea for a million and one reasons, but I like it. Keeps my code neater.

- seb

PS can someone tell me how to get blank lines to not disappear from my source on this forum?


Edited by - bsxrider on February 3, 2002 6:33:46 PM
I have often wondered about this myself, and have come up with a couple of things.

1) If you have a class (Parent) which contains an instance of Child, then the header for Child must be #included by the header of Parent. However, if Parent contains a pointer to Child, then it is better to put a forward declaration of Child in the header of Parent, and then #include the header of Child from the source file of Parent. This improves compile times.

2) A header such as can often be included in the source file rather than the header file, which is desirable. This is the case if you use it for something like cout, which does not affect the header. However, if you have functions in your class which take "file" or others as parameters, you will need to #include <iostream> in the header of you class.

The important point is to minimize the number of #includes. At compile time the compiler will only recompile those file which have changed since the last compilation. If a header file changes and is included by all your source files, they will all need to be recompiled.

As for how pre-compiled headers change the situation, I don''t know as to my knowledge they are just a vc++/windows thing. However, presumably they won''t be available for someone else compiling your program, so they won''t help thier compile time.

Hope that helps.
To summerise, #include in the source file if you can and in the header if you have to.
I seem to recall somewhere I saw someone had included all the stardard files in the pre-compile header file . I just seem strange that one moment an include file is not need as part of the header, the next minute it is required?
I don''t know what you mean: you only have to include a header file when a bit of code depends on it. That doesn''t change randomly.

david_williams summed the issue up perfectly.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

This topic is closed to new replies.

Advertisement