labyrinth
labyrinth

Reputation: 13966

How can I see the location of a header included with #include?

I'd like to be able to see which header is actually included when I compile. For example, I have two very different check.h files (one is a linux-header thing, the other from the unit test system).

Is there a setting in gcc or some language command/macro that would show where header files are being included from?

Upvotes: 2

Views: 197

Answers (2)

nneonneo
nneonneo

Reputation: 179717

You can try adding -MD to your compilation command. This generates a dependency file (suitable for Make) which will tell you all of the include files that your source code depends on.

This can be added to an existing compile command without fear of breaking the compilation, since it generates the dependency file as a side effect of normal compilation.

Upvotes: 2

dreamlax
dreamlax

Reputation: 95405

You can use the -E flag.

gcc -E source.c

This will show you the “annotated” preprocessed source, including the absolute paths of headers included using <> and relative paths of headers included using "". Keep in mind that it will be a lot to trudge through, especially if you include a lot of system headers (which in turn include implementation-specific headers etc.).

Using grep, you could filter these results with:

gcc -E source.c | grep '^# 1 '

The # n is an annotation describing the line number of the currently-included file, which is always # 1 at the beginning of a file.

Upvotes: 3

Related Questions