在 Visual Studio 中显示 C++ 文件的 #include 层次结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1137966/
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-27 18:59:25  来源:igfitidea点击:

Displaying the #include hierarchy for a C++ file in Visual Studio

c++visual-studioinclude

提问by RichieHindle

Problem:I have a large Visual C++ project that I'm trying to migrate to Visual Studio 2010. It's a huge mix of stuff from various sources and of various ages. I'm getting problems because something is including both winsock.hand winsock2.h.

问题:我有一个大型 Visual C++ 项目,我正在尝试将其迁移到 Visual Studio 2010。它包含来自不同来源和不同时代的大量内容。我遇到了问题,因为有些东西同时包括winsock.hwinsock2.h

Question:What tools and techniques are there for displaying the #includehierarchy for a Visual Studio C++ source file?

问题:有哪些工具和技术可以显示#includeVisual Studio C++ 源文件的层次结构?

I know about cl /Pfor getting the preprocessor output, but that doesn't clearly show which file includes which other files (and in this case the /Poutput is 376,932 lines long 8-)

我知道cl /P获取预处理器输出,但这并没有清楚地显示哪个文件包含哪些其他文件(在这种情况下,/P输出为 376,932 行长 8-)

In a perfect world I'd like a hierarchical display of which files include which other files, along with line numbers so I can jump into the sources:

在一个完美的世界中,我希望分层显示哪些文件包含哪些其他文件,以及行号,以便我可以跳转到源代码:

source.cpp(1)
  windows.h(100)
    winsock.h
  some_other_thing.h(1234)
    winsock2.h

回答by xtofl

There is a setting:

有一个设置:

Project Settings -> Configuration Properties -> C/C++ -> Advanced -> Show Includes

项目设置 -> 配置属性 -> C/C++ -> 高级 -> 显示包含

that will generate the tree. It maps to the compiler switch /showIncludes

这将生成树。它映射到编译器开关/showIncludes

回答by Kim Gr?sman

The compiler also supports a /showIncludes switch -- it doesn't give you line numbers, but can give a pretty comprehensive view of which includes come from where.

编译器还支持 /showIncludes 开关——它不给你行号,但可以给出一个非常全面的视图,包括从哪里来。

It's under Project Settings -> Configuration Properties -> C/C++ -> Advanced -> Show Includes.

它位于项目设置 -> 配置属性 -> C/C++ -> 高级 -> 显示包含下。

回答by Colin Desmond

We have found IncludeManagerto be a very powerful tool. It is not free (but not expensive) and it allowed us to get a grip of our Include issues and drop our compile time from 50 minutes to 8 minutes by pruning out large chunks of includes we weren't using.

我们发现IncludeManager是一个非常强大的工具。它不是免费的(但不贵),它让我们能够解决我们的 Include 问题,并将我们的编译时间从 50 分钟减少到 8 分钟,因为它删除了我们没有使用的大块包含。

回答by Paul

Not as good as gcc's hierarchical include feature, which shows the direct-line inclusion hierarchy in the case of an error. The "show includes" option in VS shows everything, which is overkill when debugging hierarchical include file problems.

不如gcc的hierarchical include特性,在出错的情况下直接显示包含层次。VS 中的“显示包含”选项显示了所有内容,这在调试分层包含文件问题时显得有些矫枉过正。

回答by Fantastic Mr Fox

There is now a plugin for Visual Studio called IncludeToolbox. It can list your dependent includes and do more things like a random remove and compile to see if that include was required.

现在有一个名为IncludeToolbox 的Visual Studio 插件。它可以列出您的依赖包含并执行更多操作,例如随机删除和编译以查看是否需要该包含。

回答by Daniel F. Thornton

Hereis a good 3rd-party, FOSS tool. You can export results to XML, which will include data on number of occurrences and line numbers.

是一个很好的第 3 方 FOSS 工具。您可以将结果导出到 XML,其中将包括出现次数和行号的数据。

回答by Agnel Kurian

回答by polyglot

cl /Pshould show you the line numbers, such that you can tell the context of where a header file is being included from.

cl /P应该显示行号,这样你就可以知道头文件从哪里包含的上下文。

If you grep out the lines with ...

如果你用...

grep "^#line" file.i

grep "^#line" 文件.i

... then you should have a pretty clean indication of what files were encountered in order by the preprocessor.

...那么您应该对预处理器按顺序遇到哪些文件有一个非常清晰的指示。

If it's a one off incident this should be a pretty quick diagnostic.

如果这是一次性事件,这应该是一个非常快速的诊断。

回答by Santiago Villafuerte

I use Doxygenand GraphViz.

我使用DoxygenGraphViz

enter image description here

在此处输入图片说明

Install both. Make sure to select GraphViz as the tool to generate the hierarchy diagrams. Select "Use dot tool from the GraphVix package".

安装两者。确保选择 GraphViz 作为生成层次结构图的工具。选择“使用 GraphVix 包中的点工具”。

Also make sure to include the binary directory from GraphViz into your PATH environment variable.

还要确保将 GraphViz 中的二进制目录包含到您的 PATH 环境变量中。