"Deep" header dependency analysis
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.