190

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • This is similar to (but not a duplicate of) http://stackoverflow.com/questions/74326/how-should-i-detect-unnecessary-include-files-in-a-large-c-project – David Stone Oct 09 '13 at 17:36
  • 5
    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 Oct 13 '14 at 08:07
  • 92
    Why do I keep finding "off-topic" questions so helpful? – jfritz42 Sep 30 '15 at 16:08
  • 6
    @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 Jul 14 '16 at 01:12
  • 3
    I wanted to note that there are plenty more tools for dealing with `#include` dependencies like [cpp-dependencies](https://github.com/tomtom-international/cpp-dependencies), [iwyu](https://include-what-you-use.org/), and [dep-matrix](https://github.com/NavicoOS/dep-matrix/blob/master/dependencydatabase.py) which is a pretty naive tool written in python. – nonsensickle Jan 18 '17 at 10:24
  • 3
    @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 Jul 19 '17 at 05:33
  • There's a new tool called [`cpp_dependency_graph`](https://github.com/shreyasbharath/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. – thegreendroid Apr 11 '18 at 09:06
  • I wouldn't mind calling the question "off-topic" if we were still allowed to post new answers. – TamaMcGlinn Aug 06 '20 at 09:23
  • @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? – TamaMcGlinn Aug 10 '20 at 06:46

10 Answers10

171

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.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
KeithB
  • 16,577
  • 3
  • 41
  • 45
  • 74
    `-H` even gives a tree! – SamB Oct 13 '11 at 03:06
  • 35
    `-MM` skips system headers – TheJosh Feb 06 '14 at 22:25
  • 3
    Also with the `-o` option the compiler gonna write the output to file instead of stdout. – Hi-Angel Sep 01 '14 at 07:13
  • 2
    @SamB This only works if there are no errors, and prints to `stderr` instead of `stdout`. Otherwise, this option is more general. – user877329 Jun 02 '16 at 12:34
  • I would give a thousand points to this- – CPPDVL Aug 05 '21 at 04:45
  • clang also supports this very same option. Check out [clang's command line argument page](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-m). – RAM Jul 16 '22 at 07:04
  • This shouldn't be the accepted answer because it answers a different question. The question was for the inverse : what files use the specified header, not what files are used by the specified file. – Griffon26 Jan 26 '23 at 11:49
96

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

Neysor
  • 3,893
  • 11
  • 34
  • 66
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
30

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.

albert
  • 8,285
  • 3
  • 19
  • 32
Matt Dillard
  • 14,677
  • 7
  • 51
  • 61
  • 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 Jan 02 '21 at 10:29
20

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.

math
  • 8,514
  • 10
  • 53
  • 61
Brian Stewart
  • 9,157
  • 11
  • 54
  • 66
  • 2
    This tool works exceptionally well. I had trouble with g++'s -M and doxygen. – sleeparrow Sep 09 '16 at 19:13
  • 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. – KRoy Feb 13 '18 at 23:08
14

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

Neysor
  • 3,893
  • 11
  • 34
  • 66
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
13

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.

Allbite
  • 2,367
  • 1
  • 24
  • 22
7

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.)

cxw
  • 16,685
  • 2
  • 45
  • 81
5

You can also check out makedepend:

http://en.wikipedia.org/wiki/Makedepend

http://www.xfree86.org/current/makedepend.1.html

dwj
  • 3,443
  • 5
  • 35
  • 42
4

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

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Xavier Nodet
  • 5,033
  • 2
  • 37
  • 48
2

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.

svec
  • 3,477
  • 2
  • 27
  • 28