C++ 如何判断头文件是从哪里包含的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5834778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:57:56  来源:igfitidea点击:

How to tell where a header file is included from?

c++cgccincludeg++

提问by harschware

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

我如何知道 g++ 在哪里能够找到包含文件?基本上如果我

#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.

g++ 将扫描搜索路径,使用任何包含选项来添加或更改路径。但是,最终,有没有办法告诉我 g++ 选择编译的 foo.h 的绝对路径?如果在无数的搜索路径中存在多个 foo.h,则尤其重要。

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?

缺少实现这一点的方法......有没有办法让 g++ 告诉我在包含默认值和所有包含选项之后它的最终搜索路径是什么?

采纳答案by Sodved

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

这将提供 make 依赖项,其中列出了包含文件的绝对路径:

gcc  -M showtime.c

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

如果您不希望系统包含(即#include <something.h>),请使用:

gcc  -MM showtime.c

回答by Bulletmagnet

g++ -H ...

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

还将以一种格式打印包含文件的完整路径,该格式显示哪个标题包含哪个

回答by wallyk

Sure use

肯定用

g++ -E -dI  ... (whatever the original command arguments were)

回答by Jonathan Leffler

If you use -MMor 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 -dIsolution).

如果您使用-MM或 相关选项之一(-M等),您只会获得包含的标头列表,而没有所有其他预处理器输出(您似乎可以通过建议的g++ -E -dI解决方案获得)。