0

I am working on a project where I am supposed to put all my #includes in a file called precomp.h, and then only #include that file in my .cpp files.

The problem is that I have two classes that refer to each other, which does not work. My precomp.h looks like this:

...
#include "class1.h" // <-- uses class2, does not compile
#include "class2.h" // <-- uses class1
...

Of course switching them also doesn't work. How do I solve this issue in a 'proper' way?

  • 1
    I'd say that the "proper" way would be to not put all your `#include`s in a single header file, and then only `#include` that file in my `.cpp` files. So I find your requirement questionably at best. *Note: the circular inclusion problem might be independent of this restriction. However, your question suggests that it is not. Have you tried to make your files work without your project's limitation?* – JaMiT May 28 '21 at 19:56
  • 1
    The circular dependency is in the headers themselves. Consumption of both by precomp.h or any other file is the trigger that exposes that broken architecture. The only way to solve this is breaking the circular dependency by using forward-declarations of class2 dependencies in class1.h, and move any inline code that otherwise-resides (if any) there into a proper translation unit. – WhozCraig May 28 '21 at 19:56
  • Search the internet for "c++ include guard" and verify that all your include files have some sort of include guard. – Thomas Matthews May 28 '21 at 20:02
  • IMHO, your project is worthless. If you make any changes to the file in the precompiled header, the precompiled header needs to be rebuilt; no savings in build time. You'll only gain significant build time savings if you have a huge project (like 100's of files). Usually on those size projects, you perform a rebuild before making a release, so there isn't really any savings. If your build process is measured in hours, then you make have build time savings by using precompiled headers. I find they are annoying at best and at worst cause compilation headeaches. – Thomas Matthews May 28 '21 at 20:08

0 Answers0