I work on a mid-sized C/C++ project to which I've already applied Doxygen+Graphviz. Its header graphs are useful, but they only show relationships based on #include
. I'm interested in finding a (preferably linux-based) tool that analyzes file dependencies based not just on #include
, but on actual symbol usage. For example, such a tool would not only show that a.cpp
includes b.h
, but that a.cpp
uses SomeClass
that's declared in c.h
included by b.h
. It'd also be able to suggest header includes that could be pruned.
I've used Include What You Use before with pretty good results. It uses Clang to parse the C++ code and suggest forwards declarations to add and header files to remove.
One drawback is that it makes assumptions about the layout of your code - basically the Google coding standards. So it will only look at SomeFile.h
if you have a file called SomeClass.cpp
. Also the suggested includes use full paths from the root of your project (so #include "src/SomeClass.h"
instead of #include "SomeClass.h"
). In the end I changed my code to this convention anyway as it avoids ambiguity, but it needs a heads up in case you try it.
Usually you can just set CC=include-what-you-use
and rebuild to get the results - it uses all the clang machinery to parse -I
include arguments. There's a python program that uses the result to automatically update your #include lines.
EDIT:
Another tool that is not as sophisticated, but is simpler to set up and can suggest #include
s to remove is deheader. It works by copying your C++ file to a temporary location, removing an #include
and recompiling. If the recompile works, then it's safe to remove that header file. What it won't do is suggest forward declarations or anything fancy, but it can cut down on needless include lines in your implementation files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With