192

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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 5
    For people that looks for a GCC/Clang solution, use `gcc -H -fsyntax-only ...` to output the hierarchy. Credits to https://stackoverflow.com/a/18593344/427545 – Lekensteyn Apr 18 '18 at 10:21

10 Answers10

286

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

janisozaur
  • 842
  • 7
  • 10
xtofl
  • 40,723
  • 12
  • 105
  • 192
  • That's just what I was looking for (barring line numbers, but you can't have everything). Silly me, I was looking under the Preprocessor options. 8-) Thanks! – RichieHindle Jul 16 '09 at 14:48
  • 25
    Note: the hierarchy can be seen in the Output window. – CannibalSmith Jul 30 '09 at 07:12
  • 3
    If anybody's interested: even if you select the Clang [platform toolset](http://msdn.microsoft.com/en-us/library/vstudio/ff770576(v=vs.100).aspx), you can still "show includes" if you add `-H` in `C/C++ -> Command Line - Additional Options` – wip Sep 10 '13 at 06:24
  • 7
    Still not as good as gcc's "included from" feature, which shows the direct include hierarchy related to a compile-time error, and which also displays line numbers. – Paul Dec 16 '13 at 10:11
  • 1
    What bothers me is that if I select the generate preprocessed file there are files mentioned that are not shown when I turned on show includes so I don't know where the problem comes from... – Martin Kosicky Jan 03 '17 at 06:52
  • 2
    I made a quick regex that strips the Visual Studio includes (anything under Program Files (x86)). You can copy+paste your output window into an app like Notepad++ and do a regex find and replace with blank to strip all VS includes from your tree: `1>\s*Note: including file:\s*C:\\Program Files \(x86\).*(\r\n|\n|$)` – Slate Mar 24 '17 at 13:04
  • 3
    Note that while /showIncludes can be toggled for an individual source file, it will have no effect unless /showIncludes is set at the project level. – David Carr Jul 04 '17 at 23:15
  • I wish this had a proper UI in VS, super handy. – sleep Jul 19 '18 at 05:59
  • 1
    Two more tips: **(1)** Make sure Configuration and Platform in the properties dialog match what you're building - for some reason they often don't, and then you don't see the expected output when you build. **(2)** Enabling this option serializes your build (as though you'd set *Tools* > *Options* > *Projects and Solutions* > *Build and Run* > *maximum number of parallel project builds* to **1**), so there's a cost to leaving it turned on. – Scott Smith Oct 18 '21 at 19:54
  • it dumps all the includes, not just the direct path and this answer is still the best there is well over a decade after – jozxyqk May 23 '23 at 01:22
  • @jozxyqk not using VStudio for a while now, but indeed, that's ridiculous. I remember copy-pasting the output in an editor and manually deleting irrelevant sections to zoom in on what matters. Also... much of my SO reputation is thanks to this anwser - a real moneycow. – xtofl May 24 '23 at 05:42
23

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.

Kim Gräsman
  • 7,438
  • 1
  • 28
  • 41
16

We have found IncludeManager to be a very powerful tool. It is not free (but not expensive) out of development and free now. It only supports Visual Studio 2005 through 2013.

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.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Colin Desmond
  • 4,824
  • 4
  • 46
  • 67
  • 5
    Yow! I ran IncludeManager on the offending file, and it produced a graph that made me laugh out loud. By my calculations I would need a 400" monitor to see the whole thing. I think we're beyond its power to help. 8-) – RichieHindle Jul 17 '09 at 22:35
  • 3
    Update - the IncludeManager parent company, ProFactor (www.profactor.co.uk) has gone out of business, but is providing its most recent releases for free from the above website. The down side is it only works on full versions of Visual Studio from VS2005 to VS2013. – Dana Jun 29 '18 at 01:53
  • 2
    IncludeManager requires Visual Studio.NET 2005 to 2013, so pretty much not usable anymore. – Pierre Sep 22 '21 at 16:53
7

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.

Paul
  • 443
  • 8
  • 18
5

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
3

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

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41
  • 1
    The tools is from 2003 and uses DSW files as input. I doubt it could catch up with the latest versions of C++ (C++11, C++17, C++20) and Visual Studio (.vsproj) – Thomas Weller Dec 15 '22 at 06:54
3

Try redhat Source-Navigator for a more graphical solution.

Community
  • 1
  • 1
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
3

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.

  • I tried this, but I have not been able to get a way for this to show me the include header hierarchy. I can see class X derives from class Y and things like that in graphviz, but not the includes list. Is there some hidden knob somewhere that I missed? – Conor Cunningham MSFT Nov 06 '21 at 22:15
  • I see. GraphViz will only show what you mentioned graphically (class hierarchies). This is an example from Doxygen: https://www.doxygen.nl/manual/examples/diagrams/html/hierarchy.html If you are not working with classes and need a different list perhaps you can try another answer. I will update the answer. Thanks. – Santiago Villafuerte Nov 06 '21 at 22:33
  • 1
    yeah, it's really useful for the class hierarchy problem - I love the graphs it generates. However, I was looking for the header include graph which is what i think the OP had listed here. I'll try some of the other questions. Thanks for forcing me to get this working (though it crashes on my whole code base as it is quite large, it works great on subsets) – Conor Cunningham MSFT Nov 08 '21 at 14:12
1

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.

polyglot
  • 2,031
  • 2
  • 20
  • 28
  • 2
    Sure, but that gives me eight thousand lines of unstructured output, with no hierarchy. – RichieHindle Jul 16 '09 at 14:46
  • I'd been looking through the cl /P output for quite long enough, and wondered whether there was a better tool for the job. Now I've discovered that there is, which is great. The question *is* general, and the answers will be here on SO forever, for others to find. – RichieHindle Jul 16 '09 at 14:55
0

Just press Alt-Shift-H in JetBrains Rider and you get an interactive includes hierarchy

Rider Include Hierachy

Jonathan
  • 6,741
  • 7
  • 52
  • 69