views:

238

answers:

6

I am making a small C++ framework, which contains many .h and .cpp.

I have created a general include which include all my .h file such as:

framework.h

#include "A.h"
#include "B.h"
#include "C.h"

each .h header are protected with include guard such as

#ifndef A_HEADER
#define A_HEADER
...
#endif

The issues is, I would like to be able to include "framework.h" inside all the sub .h such as, but it cause lots of compiler error:

#ifndef A_HEADER
#define A_HEADER

#include "framework.h"
...
#endif

If instead I use the real header file for each sub header, and the framework.h for what ever use my framework it works fine..

I would just like to include the main header inside all my sub .h so I dont need to include all the dependency everytime.

Thanks :)

A: 

i would recommend using #pragma once, and placing that at the top of all of your header files (framework.h, A.h, B.h, and C.h).

Although, if you'd rather, I think you could fix your problem by simply putting an include guard in framework.h as well.

Andrew Garrison
Please don't use #pragma once in frameworks, we ordinary folk hate those deprecation warnings from g++.
avakar
Not an issue anymore. From Wikipedia:"However, with the 3.4 release of GCC, the #pragma once handling code was fixed to behave correctly with symbolic and hard links. The feature was "un-deprecated" and the warning removed"
Andrew Garrison
@Andrew Garrison, I stand corrected.
avakar
+2  A: 

Just protect the main header with include guards too:

#ifndef FRAMEWORK_H
#   define FRAMEWORK_H
#   include <A.h>
#   include <B.h>
#   include <C.h>
#endif

That will prevent recursive inclusion.

Nikolai N Fetissov
No circular dependency issues, but undefine stuff, just like it if I forgot to include a .h. Mosty for thing are are defined inline in some header files.
JP
+4  A: 

Basically what your doing is #include "A.h" in framework.h and #include "framework.h" in A.h. This causes cyclic dependency of the header files and you will get errors such as undefined class A. To solve this, use forward declarations in header file and #include only in corresponding cpp file. If that is not possible then I don't see any other option other than including individual header files.

Naveen
A: 

I guess you have a dependency between - say B and C such that B depends on C, but in framework.h C is included after B.

ur
You are right but Naveen answered first
JP
A: 

Circular includes are generally a bad idea in C++. While having header guards will prevent the preprocessor from going into infinite loop (or throwing an error because of this), you will get unexpected compiler errors, because at some point a header file will not be included when if you think it is.

You should include A.h, B.h and C.h from framework.h, and in A.h, do not include framework.h, just forward declare the classes you use from it. Or do it the other way around: include framework.h from A.h, B.h and C.h, and forward declare classes in framework.h. And, of course, put every code that would require any more detailed declarations than for example class A to the .cpp files.

petersohn
+2  A: 

You should not including the main header file inside the sub-header files. It should be used to make user's life easier, not yours.

Instead do following:

1) Make forward definitions of all you need in the related sub-header files.

2) Include only needed sub-header files inside CPP files.

3) When using your framework inside an application code (for example), then you could include the main framework header file.

boko
+1 for the process
neuro