Multiple headers

Started by
5 comments, last by Evolution 23 years, 11 months ago
Is it smart to include all your needed header files in ONE single header file. Then just include that single header in all the other files? i.e // headers.h #ifndef HEADERS #define HEADERS #include #include #include // etc... #endif
Advertisement
Thats exactly what I do, and it works great. However, be carefull with your main file and if you have any global variables. Here is what I do.

int main.cpp, define any globabl variables:
int variable_x = 10;

then you have your other source files:
source1.cpp
source2.cpp
source3.cpp ect....

then, have a Global_Variables.h header:
extern int variable_x;

Then you also have your other headers;
header1.h
header2.h
header3.h ect....

Then create a header for all your headers for your main.cpp file, for this dont include the Global_Variables.h. But for your .cpp files, have another header that does include the Global_variables.h

in main.h
#include "header1.h"
#include "header2.h"
#include "header3.h"

and thus in main.cpp
#include "main.h"


for the other source files have
in Other_Sources.h
#include "Global_Variables.h"
#include "header1.h"
#include "header2.h"
#include "header3.h"

and then in all other source files have
#include "Other_Sources.h"

Possibility
It''s possible, but it''s not considered good coding practice. I''m not a compiler expert, but using those includes basically tells the preprocessor to append all those include files together to create one great big file for the compiler. If you include all header files in all source files, you create a lot of overhead for the compiler. For small projects this may not be a big deal but for larger projects in can REALLY increase the time it takes to build the project, not to mention you run the risk of other hard to track problems. You''re a lot safer going through the extra work to include only those headers necessary.
Thanks for the help, it''s appreciated!
Not only does it make the compile time for each file increase, but it can cause spurious recompiles. That is to say if one of your .cpp files includes a header, but doesn''t really use it, if that header changes it''ll be recompiled for no good reason.
The spurious recompiles issue is a big one when you start getting large projects. I spent many days separating out all my header files in one project just to get round this problem. It is still so badly organised that changing most headers forces me to recompile half the program: but that is still twice as convenient as recompiling all the program.
Kylotan- i had the exact same problem, but i spent a few days re-organising, and now i only gotta fully recompile when i change the pch header ( some header changes incur a ~30% recompile, but thats unavoidable in my case )

oh, and on the main subject matter: dont do it! is it really so hard to spend a minute including the headers you need? dont be lazy

Edited by - POINT on May 21, 2000 5:35:28 AM

This topic is closed to new replies.

Advertisement