Tool to track #include dependencies

78,830

Solution 1

If you have access to GCC/G++, then the -M option will output the dependency list. It doesn't do any of the extra stuff that the other tools do, but since it is coming from the compiler, there is no chance that it will pick up files from the "wrong" place.

Solution 2

Thanks to KeithB. I looked up the docs for cl.exe (VS2008) and found the /showIncludes flag. From the IDE, this can be set from the property page of any CPP file.

Screen shot

Solution 3

For a heavy weight solution, you should check out doxygen. It scans through your code base and comes up with a website, effectively, that documents your code. One of the many things it shows is include trees.

If you were looking to be able to plug the output of this tool into some other process, then this may not work for you (although doxygen does output to other formats, I'm not real familiar with that feature). If you simply want to eyeball the dependencies, though, it should work great.

Solution 4

I've played around with a tool called cinclude2dot. It was pretty useful in getting a handle on a rather large codebase when I came to work here. I've actually thought about integrating it into our daily build eventually.

Solution 5

First, cinclude2dot.pl is a perl script which analyses C/C++ code and produces a #include dependency graph as a dot file for input into graphviz.

http://www.flourish.org/cinclude2dot/

If you don't want to go the way of that sort of manual tool, then the hands-down by far winner is in my opinion a tool known as "IncludeManager" from ProFactor.

http://www.profactor.co.uk/includemanager.php

There's a free trial, and it is awesome. It's a plug-in for Visual Studio that's totally integrated so double clicking on something over here takes you to the place where it is included over there.

Tooltip mouseovers give you all the info you would want, and it lets you drill down / up, remove whole subtrees you don't care about, view representations other than graphs, cycle through a list of matches for this and that, it's wonderful.

If you're quick about it, you can refactor the #include structure of a large projects before the trial runs out. Even so, it doesn't cost much, about $35 per license.

For what it does, it is just about perfect. Not only #include graphs but also cross project dependencies of shared files, impact on build times, detailed properties in grids, perfect.

Share:
78,830

Related videos on Youtube

Agnel Kurian
Author by

Agnel Kurian

Software Engineer with 18+ years of experience developing software in a wide range of areas: desktop, web, user interfaces, 2D/3D graphics, geometry, encryption and even structural analysis! I have a degree in Civil Engineering and a diploma from NIIT. I'm very comfortable on Linux. I like solving interesting problems.

Updated on December 13, 2020

Comments

  • Agnel Kurian
    Agnel Kurian over 3 years

    Any good suggestions? Input will be the name of a header file and output should be a list (preferably a tree) of all files including it directly or indirectly.

    • David Stone
      David Stone over 10 years
      This is similar to (but not a duplicate of) stackoverflow.com/questions/74326/…
    • Fantastory
      Fantastory over 9 years
      It is not about a "favorite" includes are shown nicely in gcc, but msvs does not. So we (I) are looking for any tool.
    • jfritz42
      jfritz42 over 8 years
      Why do I keep finding "off-topic" questions so helpful?
    • Totoro
      Totoro almost 8 years
      @jfritz42: This should be given the "Comment of the Year" award! How can one moderator label a question "off topic" when there are so many topics and so many users?
    • nonsensickle
      nonsensickle over 7 years
      I wanted to note that there are plenty more tools for dealing with #include dependencies like cpp-dependencies, iwyu, and dep-matrix which is a pretty naive tool written in python.
    • Andreas
      Andreas almost 7 years
      @jfritz42 The numbers are clear: 123 votes and 62 favourites. Many people see it this way. Weird SO standards. I also doubt that such questions would have the same great answers on SuperUser.
    • thegreendroid
      thegreendroid about 6 years
      There's a new tool called cpp_dependency_graph, it's still in early development but I have a number of improvements planned. Disclaimer - I am the author of this tool.
    • TamaMcGlinn
      TamaMcGlinn almost 4 years
      I wouldn't mind calling the question "off-topic" if we were still allowed to post new answers.
    • TamaMcGlinn
      TamaMcGlinn almost 4 years
      @Alex-Myers I tried to edit this question to be on-topic. As far as I can see, i did the minimal; remove 'tool suggestions' and add 'what have you tried so far'. If you disagree with my way of fixing the question, could you fix it yourself?
  • Markus Joschko
    Markus Joschko about 15 years
    This is extremely useful in solving some very hard compile errors/warnings. Thanks a lot!
  • fmuecke
    fmuecke over 14 years
    This is also extremely handy when trying to optimize precompiled headers!
  • SamB
    SamB over 12 years
    -H even gives a tree!
  • yaobin
    yaobin almost 11 years
    When working in VS, I think this is the quickest solution to solve my problem~ :-)
  • TheJosh
    TheJosh over 10 years
    -MM skips system headers
  • Hi-Angel
    Hi-Angel over 9 years
    Also with the -o option the compiler gonna write the output to file instead of stdout.
  • abergmeier
    abergmeier over 8 years
    This does not seem to work, if some header does include a std c header e.g. math.h
  • user877329
    user877329 almost 8 years
    @SamB This only works if there are no errors, and prints to stderr instead of stdout. Otherwise, this option is more general.
  • sleeparrow
    sleeparrow over 7 years
    This tool works exceptionally well. I had trouble with g++'s -M and doxygen.
  • smwikipedia
    smwikipedia almost 7 years
    I have successfully used IncludeManager in my C project. I am using Visual Studio 2013.
  • KRoy
    KRoy over 6 years
    I wrote a python script to read the output of cinclude2dot and get all the dependency in a map and then do depth-first-traversal to finally output a forest of sources. The forest that does not have any .cc/.c/.cxx file in that(only the .h files in it) may be redundant.
  • sdd
    sdd about 6 years
    Kinda suspicious.
  • skelliam
    skelliam over 5 years
    Understand is commercial, but it is incredible IMO. You can try it for free.
  • David V. Corbin
    David V. Corbin over 3 years
    How does that determine "unnecessary"??? I have "foo.cpp", it includes (directly or indirectly "bar.h"...Will the removal of bar.h cause any differences in the compiled output of foo.cpp?? If the answer to this is no, then it is an unnecessary include. This is hard.... foo.h may #define something that is #if in a completely different .h file....
  • David V. Corbin
    David V. Corbin over 3 years
    No longer at that link...
  • CPPDVL
    CPPDVL almost 3 years
    I would give a thousand points to this-