• Advertisement
Sign in to follow this  

multiple Include issue

This topic is 3317 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

Hello. I know this is a quite common subject but I just can't seem to wrapp my head around it. Lets say I got a header called Math.h and it contains Vector3, Matrix4 etc. So if I need to do som calculus I include Math.h. Now, I have a class called Core, it's the core of a 3d engine. Among other things core contains a Renderer, ModelManager and som other stuff that requiers Math.h to function. if I in Core.h include following headers: Renderer.h ModelManager.h And in both Renderer.h and ModelManager.h I've included "Math.h". This will lead to multiple includes, right?? Even core might want to have som vectors or such. I've looked around, and found that inclusion guards won't help in this case. Since I will check if Renderer is alrady defined, and if not i define it and include all headers it needs, same goes for ModelManager. Offcourse there is some simple solution for this, I just can't see it. Forward declarations won help, I need more than pointers. ************** core includes Math core includes Renderer, Renderer includes Math core includes ModelManager, ModelManagerincludes Math ************** I supose you could declare all classes in a single header, but i don't think thats a nice solution :/

Share this post


Link to post
Share on other sites
Advertisement
Multiple includes shouldn't be a problem - that is what include guards are for. The name of your problem is circular includes / circular dependencies. For information on how to resolve such an issue read this.

I would advise against having a "core.h" (reasoning).

Share this post


Link to post
Share on other sites
Quote:
Original post by HermanssoN
I've looked around, and found that inclusion guards won't help in this case.

That's exactly what include guards are for.

Share this post


Link to post
Share on other sites
Don't worry, core isn't a master header.

Well, what I meant is that it doesn't help if I inlcude the same header in several other headers.

But anyways. Tha article rip-off supplied used forward declarations as the solution. But I don't buy that in this case. A class like Vector3 must be accessible anywhere in the application. And in most 3D engines it is. But include it in every header that needs a Vector3 instance will lead ti multiple include of teh same header.

ex:

*****Class1.h************
#ifndef CLASS1_H
#define CLASS1_H

#include "Math.h" //Math will only be included if CLASS1_H is undefined

#endif
*************************

*****Class2.h************
#ifndef CLASS2_H
#define CLASS2

#include "Math.h" //Math will only be included if CLASS2_H is undefined,BUT! I't will include Math.h if CLAS2_H is undefined, even if Math.h was included when CLASS1_H wasn't defined. or is this where I'm wrong?

#endif
*************************

[Edited by - HermanssoN on January 24, 2009 5:17:28 PM]

Share this post


Link to post
Share on other sites
Including the same file multiple times, correctly handled, is not a problem. Really.

The problems are:

- lack of include guards
- circular dependencies

Does math.h have its own include guard? And I assume in your example should be:

#ifndef CLASS2_H
#define CLASS2_H // <-- extra _H

Share this post


Link to post
Share on other sites
Quote:
Original post by HermanssoN
#include "Math.h" //Math will only be included if CLASS2_H is undefined,BUT! I't will include Math.h if CLAS2_H is undefined, even if Math.h was included when CLASS1_H wasn't defined. or is this where I'm wrong?

Yes, Math.h will be included multiple times. This won't be a problem because you gave Math.h include guards, right?

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Including the same file multiple times, correctly handled, is not a problem. Really.

The problems are:

- lack of include guards
- circular dependencies

Does math.h have its own include guard? And I assume in your example should be:
*** Source Snippet Removed ***



Yes the missing H was a type error. So if Math.h has a include guard, wich it does. It's safe to include Math.h in those headers that need Math.h stuff. If so that is good news :)

Thanks a bunch!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement