How to know which headers are included without looking at the preprocessed code in GCC?
Asked Answered
N

4

9

I've got some big C programs, and I would like to know when I'm compiling this program, which header files are actually included...

The simplest solution would be to print the preprocessed code and look but do you know if there's a way to compile and at the same time showing what header files are included?

Naara answered 26/6, 2009 at 13:39 Comment(0)
R
15

Use the -M option to output the dependencies. Use -MD to generate and compile. Use -MF to redirected to a file.

Also -MM allow ignoring the system file in the dependencies list.

gcc ... -M  -MF <output_file>     # generate dependencies
gcc ... -MD -MF <output_file>     # compile and generate dependencies
Racialism answered 26/6, 2009 at 13:49 Comment(3)
but from the manual : Passing -M to the driver implies -E, and suppresses warnings with an implicit -w. So the program is not actually compiledNaara
Yes, you have to use -MD to compile and generate the dependencies at the same time.Racialism
These days (I don't know whether it applied in 2009), you can use -H too. This presents the header files in a hierarchy, so you can see which header included which others, to arbitrary depths. This can sometimes be informative — especially if your project uses ../somedir/someheader.h notations either directly in #include lines or via -I options on the command line. The same file can be included by multiple different paths.Wailoo
R
5

You can use -MD option - see man gcc for details.

Redness answered 26/6, 2009 at 13:42 Comment(0)
S
0

Increase gcc verbosity and then run it through an own made filter program?

Sprage answered 26/6, 2009 at 13:40 Comment(0)
M
0

Use gcc -M or gcc -MM. Adjust the output with sed if you like. If you use GNU make (and you should) you can wrap this up a into single tidy command.

Mccahill answered 26/6, 2009 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.