views:

165

answers:

2

Does anyone know of a tool that will analyse a C++ codebase and display a graphical representation of which files include which header files and highlight redundant includes? I've used Understand C++ but it's expensive and became very unwieldy very quickly on a large (and poorly encapsulated) codebase.

+1  A: 

try this

http://stackoverflow.com/questions/42308/tool-to-track-include-dependencies

It also sounds like a very simple exercise to write one of your own. Adding "redundant" include determination though might be more challenging. One would have to parse and follow ifdefs and the like. But for just making a tree of dependencies it is pretty straightforward.

Tim
+1  A: 

There's always the "-H" option to gcc/g++...

Eg.: % g++ -H foo.C

'-H'
     Print the name of each header file used, in addition to other
     normal activities.  Each name is indented to show how deep in the
     '#include' stack it is.  Precompiled header files are also
     printed, even if they are found to be invalid; an invalid
     precompiled header file is printed with '...x' and a valid one
     with '...!' .

Then:

  • 'sed' or 'awk' to remove the leading '...'.
  • 'sort to make same names adjacent.
  • 'uniq -c' to count names.
  • 'grep -v' to remove singletons.

As in:

%  g++ -H  foo.C |& awk '{print $2}' | sort | uniq -c | grep -v '      1 '

Or is that too linux'y / unix'y for you?

(Under windows, there's always cygwin.)

Mr.Ree