c++headerincludecircular-dependencyinclude-guards

Header guard issues with circular #include


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 :)


Solution

  • 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.