5

Say, I'd like to have a tool (or script?) taking project (or .h file) and building searchable tree of "includes" included into it (of included into of included into and so so on). Is there exist something like this? Should I write this by myself [of course I am :), but may be somebody had it already written or may be has an idea how to get it]?

bgee
  • 989
  • 2
  • 13
  • 19

6 Answers6

7

I know this is an old question, a slightly more useful output than the gcc/g++ -E output alone would also used the -H flag (instead or in addition to):

g++ -H {my -I and other flags} -E -o /dev/null file.cpp

here is a sample output, tree structure helps figure out who included what as a bonus it also lists at the bottom which files may benefit from an include guard

. generated/gen-cpp/File.h
.. /usr/include/thrift/TProcessor.h
... /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/string
.... /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/c++config.h
..... /usr/include/bits/wordsize.h
..... /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/os_defines.h
...... /usr/include/features.h
....... /usr/include/sys/cdefs.h
........ /usr/include/bits/wordsize.h
....... /usr/include/gnu/stubs.h
........ /usr/include/bits/wordsize.h
........ /usr/include/gnu/stubs-64.h
..... /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/x86_64-redhat-linux/bits/cpu_defines.h
.... /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stringfwd.h

...
nhed
  • 5,774
  • 3
  • 30
  • 44
4

Not entirely sure this is what you're after, but you can easily get a list of includes by generating the post-CPP-processed file from the base c file, and grepping out the file/line number comments, e.g., using gcc

gcc -E main.c {usual flags} | grep '#' | cut -d' ' -f3 | sort | uniq

where main.c is your base c file.

jon hanson
  • 8,722
  • 2
  • 37
  • 61
  • This is how I have done it in the past (except with MSVC). The hard part of automating is getting the correct compile flags, from either projects or makefiles) as these can effect what files are included when. – iain Aug 04 '09 at 11:15
  • Thanks. In common, "I'm after" .h file (consequence of includes) in big and old project causing failures. I am interested in MSVC, but if I not receive better answer - this will do. – bgee Aug 05 '09 at 14:34
  • 1
    The VS CL.exe compiler has /E & /P flags which generate the processed output to stdout and a file respectively. You can get grep, cut etc from one of several (free) Unix tools for Windows packages around, e.g. http://unxutils.sourceforge.net. – jon hanson Aug 05 '09 at 14:56
1

If I remember correctly, doxygen can do this.

Baumflaum
  • 749
  • 7
  • 20
sbi
  • 219,715
  • 46
  • 258
  • 445
  • How does it handle preprocessor directives and include paths? – sehe Jul 15 '11 at 08:56
  • @sehe: I haven't had the chance to play with doxygen in a long time. You will have to try. From what I remember, though, its support for weird configurations was rather impressive, and its development rapid. – sbi Jul 15 '11 at 23:23
1

Include Finder is a pretty useful tool. It has some bugs, and hasn't been updated in a while, but the author does provide the source, so you can modify it to your liking.

enter image description here

Jim Fell
  • 13,750
  • 36
  • 127
  • 202
0

Eclipse CDT has the Include Browser under Window --> Show View --> Other... --> C/C++ --> Include Browser.

Tor Klingberg
  • 4,790
  • 6
  • 41
  • 51
0

There exists a tool called include gardener, which can be found here: https://github.com/feddischson/include_gardener This gives you the include tree in dot or graphml (xml) format. But it does not consider other preprocessor statements like #if, #else, #endif.

feddischson
  • 136
  • 1
  • 8