I am looking into a big C project. The header file include are conditioned on a bunch of macro definitions. It's rather tedious to manually go through them.

I am wondering if there's a tool that can resolve all the actually included header files given the macro definitions in compiler options?

Given a .c file and the compiler options, I hope the tool can automatically walk down the include hierarchy and tell me all the actually included header files.

This question was moved from SO. The responses I got from there are:

  • A tool called coan: http://coan2.sourceforge.net/

  • with GCC, file : -M -E (without system's file -MM -E), definitions : -dM -E – BLUEPIXY 1 hour ago


We have a similar problem with ensuring that all of the includes are referenced for make and we use gcc with all of the defines and other options set and the -M family of flags to give us make format dependencies even though our code is actually built with a different compiler, (qcc), this works really well and we have an extra make target, depends, to refresh the dependency files.

The gcc -M flags respect the defines, and other compiler settings, so if you have conditional includes they will be tailored for your flags.

Some, but not all, other compilers support the same, or similar, flags but in our case while qcc is based on gcc it does not support the flags. If your compiler supports these flags you can make the (re)generation of dependencies a side effect of your main build.

A good example and discussion can be found here.


After some search, I found a solution with the Visual C++ compiler cl.exe.

The /showIncludes option:

Causes the compiler to output a list of the include files. Nested include files are also displayed (files that are included from the files that you include).

When an include file is encountered during compilation, a message is output, for example:

Note: including file: d:\MyDir\include\stdio.h

Nested include files are indicated by an indentation, one space for each level of nesting, for example:

Note: including file: d:\temp\1.h
Note: including file:  d:\temp\2.h

In this case, 2.h was included from within 1.h, hence the indentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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