3

I am having problems with a circular dependency in a head-only library for C++ that would have not been a circular dependency problem when using source files instead of making everything header-only.

The situation is equal to this:

There are four files for two classes A and B. Every class has got its header file (e.g. "A.hpp") and its implementation file (e.g. "A.tpp").

The dependencies are as follows:

  • A's header requires B' header
  • A's impl requires B's header
  • B's header requires nothing but a forward declaration to A
  • B's impl requires A's header

So in a source-based library everything would be fine with the following order of source file inclusion and compilation:

  • B.hpp
  • A.hpp
  • A.tpp or B.tpp (order here is not important)

My files are structured in this way:

File content for A.hpp:

#ifndef A_H
#define A_H
#include "B.hpp"
class A { ... };
#include "A.tpp"
#endif

File content for A.tpp:

#ifndef A_H
    #error "Do not include this file directly."
#endif
// ... some implementations for A.hpp

File content for B.hpp:

#ifndef B_H
#define B_H
class A;
class B { ... };
#include "B.tpp"
#endif

File content for B.tpp:

#include "A.hpp"
#ifndef B_H
    #error "Do not include this file directly."
#endif
// ... some implementations for B.hpp

So my question is: Is there a solution for breaking this unnessesary circular dependency which only happens due to the fact that I am using a header-only solution for my library?

robbepop
  • 119
  • 10
  • never `#include` implementation files. In `B.hpp` you need `#include "A.hpp" after `class B` definition. – twentylemon Jun 13 '15 at 16:46
  • 1
    @twentylemon I think it's templates – Christophe Jun 13 '15 at 17:06
  • 1
    The circular dependency is related to the content of your classes. It's difficult to advise on how to break it, no knowing what are the constraints and why this way of defining things bothers you. – Christophe Jun 13 '15 at 17:11
  • It's structured odd. `A.tpp` should just include `A.hpp` - let the header include all the files it needs. Also, wouldn't `#include "A.hpp" #include "A.tpp"` break? `A.tpp` needs it's own header guards. Does `A.tpp` need to include anything? Why not just implement the template functions in the header file like is common practice? – twentylemon Jun 13 '15 at 17:12
  • http://julipedia.meroh.net/2013/12/header-files-c-ipp-files.html found this article - last sentence: "And keep in mind that including ipp files from within hpp files is almost always the wrong thing to do." which I basically did and now I know why this was a terrible mistake. think I have to change how my whole project is structured in general ... thanks for your answers! – robbepop Jun 13 '15 at 18:37

0 Answers0