Search code examples
c++directory-structure

In C++, why are cyclical directory dependencies bad?


I'm asking this about a C++ project developed on Linux. Consider this:

I have two peer directories, dir1 and dir2. dir1 contains classA.h and classB.h. dir2 contains classC.h and classD.h. dir1/classA.h has an #include for dir2/classC.h. dir2/classD.h has an #include for dir1/classB.h. As a result, there is a cyclical dependency between directories dir1 and dir2. However, there are no cyclical dependencies between any classes.

I understand why cyclic dependencies are bad between classes. It seems intuitive to me that directories should also not have cyclical dependencies--however I can't figure out why this would be bad.

Anyone have an explanation?


Solution

  • They are not bad. At least not the way you stated the problem. Directories are meant to organize files, but programatically have no meaning.

    However if your directories represent separate modules (i.e. there is a generated library file for each directory), you will have linking errors.

    Because classA depends on classC, you need to build the second module in order to compile the first one. But the second module needs the first module to be built first, since classD depends on classB.