I currently have a C++ project that spans two different source control systems. Without checking in the full complete source code from the first system into the second system I plan to check in just the supporting libraries that gets built. My issue is that I will need header files from the first source control system that I use in the second source control system so that I can compile. Is there an easy way of forcing gcc to output all the names of the header files that I use when I compile under Solaris? Is there a way to generate a hierarchy of header file dependencies so that I can see which files are including which other header files? Is it possible for the hierarchy to know about the #pragma once so the dependency tree doesn't include duplicates that aren't included multiple times?

2 Answers 2


Run gcc -M to generate the list of header files used by a given source file, or gcc -MM to omit system headers. The output is in a makefile format, since the option is intended to generate the build dependencies of that source file. There are ways to tweak the output format, see the available preprocessor options in the GCC manual. The default output looks like this:

foo.o: foo.c some_header.h ../include/other_header.h \
  • Do you know if clang has an equivalent? The -MM slag doesn't seem to do anything. Nov 26, 2014 at 19:19
  • how do you tweak the output format? i can't use it all just one long line. (separating with \ when it can't fit on the same line. fortunate if all the paths you want do that) i just need a list.
    – Puddle
    Jan 30, 2019 at 9:06

This 1992 paper by some AT&T Research folks describes a tool "incl" that does this for C files, but as far as I know, "incl" never made it out to the great wide world.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .