Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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 \
share|improve this answer
Do you know if clang has an equivalent? The -MM slag doesn't seem to do anything. – Faheem Mitha Nov 26 '14 at 19:19
@FaheemMitha stackoverflow.com/questions/5584435/… – Gilles Nov 26 '14 at 19:47
Thanks, Gilles. – Faheem Mitha Nov 26 '14 at 20:01

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.