180
votes

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.

10
It is not about a "favorite" includes are shown nicely in gcc, but msvs does not. So we (I) are looking for any tool.fantastory
Why do I keep finding "off-topic" questions so helpful?jfritz42
@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?Totoro
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.nonsensickle
@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.Andreas

10 Answers

158
votes

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.

94
votes

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

30
votes

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.

21
votes

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.

14
votes

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.

13
votes

Good news: redhat Source-Navigator (runs on Windows too). Of course, compiler switches (mentioned earlier) have superior parsing and I'm not sure how this will handle MFC, Qt and their magic keywords.

redhat Source-Navigator

6
votes

Building on KeithB's answer, here is GNUmake syntax to automatically 1) generate the dependency files, 2) keep them up to date, and 3) use them in your makefile:

.dep:
    mkdir $@
.dep/%.dep: %.c .dep
    (echo $@ \\; $(CC) $(IFLAGS) -MM $<) > $@ || (rm $@; false)
.dep/%.dep: %.cpp .dep
    (echo $@ \\; $(CXX) $(IFLAGS) -MM $<) > $@ || (rm $@; false)
DEPEND := $(patsubst %.dep,.dep/%.dep,$(OBJ:.o=.dep))
-include $(DEPEND)

(Make sure to change those indents to hardtabs.)

4
votes

Understand for C++ should be able to help you: it builds a database that you can access from Perl.

2
votes

cscope (http://cscope.sourceforge.net/) does this in a standalone xterm, and also can be used inside your favorite editor - it has great emacs and vi/vim support.