cpp - dependency tree c++

Tool to track#include dependencies (8)

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.

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

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:

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

(Make sure to change those indents to hardtabs.)

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.

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.

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.

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.

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.