Circular dependency in C ++ headers. How to find?

I suppose you all know what circular dependency is in the headlines. The result of this is usually the following:

error: "MyClass" was not declared in this area

If the program is short, then it’s clear what to do. But if the program has dozens of files ...

My question is, is there any algorithm to find circular addiction? "I mean some specific steps that lead you to success, and not just" study the code until you find it. "

Maybe some kind of program that does this?

+4
source share
3 answers

The Doxygen documentation tool can generate diagrams showing dependencies. I used it to display circular dependencies between the header files of different libraries.

0
source

At least one compiler that I know (Visual C ++) has a "show includes" option that helps track the inclusion order. This will help you find out where the loop is going. If your compiler does not have this option, you can add a #pragma message (or equivalent) to the top of your files to track it.

0
source

But if the program has dozens of files ...

Then it is still short. Go to the line indicated in the compiler error message and see if the class is available here. If the problem occurs in * .cpp, #include the corresponding file. If the problem occurs in the headline, add a forward declaration ( class MyClass; ). If the forward declaration is not enough, #include a file declaring myclass. If this causes a circular dependency, then you have too many types for each header. Divide the heading into several small headings. One way to do this is to have one class for each title for the entire program, but in many scenarios this may be redundant.

-1
source

Source: https://habr.com/ru/post/1410871/


All Articles