How to tell where a header file is included from?

2019-01-12 16:17发布

How can I tell where g++ was able to find an include file? Basically if I

#include <foo.h>

g++ will scan the search path, using any include options to add or alter the path. But, at the end of days, is there a way I can tell the absolute path of foo.h that g++ chose to compile? Especially relevant if there is more than one foo.h in the myriad of search paths.

Short of a way of accomplishing that... is there a way to get g++ to tell me what its final search path is after including defaults and all include options?

标签: c++ c gcc include g++
4条回答
神经病院院长
2楼-- · 2019-01-12 17:03

Sure use

g++ -E -dI  ... (whatever the original command arguments were)
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-12 17:07
g++ -H ...

will also print the full path of include files in a format which shows which header includes which

查看更多
虎瘦雄心在
4楼-- · 2019-01-12 17:10

If you use -MM or one of the related options (-M, etc), you get just the list of headers that are included without having all the other preprocessor output (which you seem to get with the suggested g++ -E -dI solution).

查看更多
Fickle 薄情
5楼-- · 2019-01-12 17:14

This will give make dependencies which list absolute paths of include files:

gcc  -M showtime.c

If you don't want the system includes (i.e. #include <something.h>) then use:

gcc  -MM showtime.c
查看更多
登录 后发表回答