shambolic shambolic - 1 year ago 85
C++ Question

How should I detect unnecessary #include files in a large C++ project?

I am working on a large C++ project in Visual Studio 2008, and there are a lot of files with unnecessary

#include
directives. Sometimes the
#include
s are just artifacts and everything will compile fine with them removed, and in other cases classes could be forward declared and the #include could be moved to the
.cpp
file. Are there any good tools for detecting both of these cases?

Answer Source

While it won't reveal unneeded include files, Visual studio has a setting /showIncludes (right click on a .cpp file, Properties->C/C++->Advanced) that will output a tree of all included files at compile time. This can help in identifying files that shouldn't need to be included.

You can also take a look at the pimpl idiom to let you get away with fewer header file dependencies to make it easier to see the cruft that you can remove.