Tool to track #include dependencies
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.
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.
Related videos on Youtube
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, 2020Comments
-
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 over 10 yearsThis is similar to (but not a duplicate of) stackoverflow.com/questions/74326/…
-
Fantastory over 9 yearsIt is not about a "favorite" includes are shown nicely in gcc, but msvs does not. So we (I) are looking for any tool.
-
jfritz42 over 8 yearsWhy do I keep finding "off-topic" questions so helpful?
-
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 over 7 yearsI 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 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 about 6 yearsThere'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 almost 4 yearsI wouldn't mind calling the question "off-topic" if we were still allowed to post new answers.
-
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 about 15 yearsThis is extremely useful in solving some very hard compile errors/warnings. Thanks a lot!
-
fmuecke over 14 yearsThis is also extremely handy when trying to optimize precompiled headers!
-
SamB over 12 years
-H
even gives a tree! -
yaobin almost 11 yearsWhen working in VS, I think this is the quickest solution to solve my problem~ :-)
-
TheJosh over 10 years
-MM
skips system headers -
Hi-Angel over 9 yearsAlso with the
-o
option the compiler gonna write the output to file instead of stdout. -
abergmeier over 8 yearsThis does not seem to work, if some header does include a std c header e.g.
math.h
-
user877329 almost 8 years@SamB This only works if there are no errors, and prints to
stderr
instead ofstdout
. Otherwise, this option is more general. -
sleeparrow over 7 yearsThis tool works exceptionally well. I had trouble with g++'s -M and doxygen.
-
smwikipedia almost 7 yearsI have successfully used IncludeManager in my C project. I am using Visual Studio 2013.
-
KRoy over 6 yearsI 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 about 6 yearsKinda suspicious.
-
skelliam over 5 yearsUnderstand is commercial, but it is incredible IMO. You can try it for free.
-
David V. Corbin over 3 yearsHow 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 over 3 yearsNo longer at that link...
-
CPPDVL almost 3 yearsI would give a thousand points to this-