How to list all C++ header files included by a source or header file (recursively)

Use the C preprocessor to list includes:

gcc_includes_cmd.sh
gcc -E -M myfile.c

The output looks like:

gcc_includes_output.txt
myfile.o: myfile.c header1.h /usr/include/stdio.h /usr/include/stdlib.h

For recursive listing, use -M or -MM options. You can also write a small script to parse the output into a list.


Check out similar posts by category: C/C++