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

C++Visual StudioInclude

C++ Problem Overview


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.h and winsock2.h.

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

I know about cl /P for getting the preprocessor output, but that doesn't clearly show which file includes which other files (and in this case the /P output is 376,932 lines long 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

C++ Solutions


Solution 1 - C++

There is a setting:

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

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

Solution 2 - C++

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.

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

Solution 3 - C++

We have found IncludeManager to 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.

Solution 4 - C++

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.

Solution 5 - C++

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.

Solution 6 - C++

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

Solution 7 - C++

Solution 8 - C++

I use Doxygen and GraphViz for class hierarchy graphics and a dependency tree in text for those.

Doxygen: Class Hierarchy

Doxygen diagrams: include hierarchy (classes)

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

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

Solution 9 - C++

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

If you grep out the lines with ...

grep "^#line" file.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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRichieHindleView Question on Stackoverflow
Solution 1 - C++xtoflView Answer on Stackoverflow
Solution 2 - C++Kim GräsmanView Answer on Stackoverflow
Solution 3 - C++Colin DesmondView Answer on Stackoverflow
Solution 4 - C++PaulView Answer on Stackoverflow
Solution 5 - C++Fantastic Mr FoxView Answer on Stackoverflow
Solution 6 - C++Daniel F. ThorntonView Answer on Stackoverflow
Solution 7 - C++Agnel KurianView Answer on Stackoverflow
Solution 8 - C++Santiago VillafuerteView Answer on Stackoverflow
Solution 9 - C++polyglotView Answer on Stackoverflow