Jump to content
  • Advertisement

Archived

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

Evolution

Multiple headers

This topic is 6914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!