RichieHindle
RichieHindle

Reputation: 281525

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

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

Upvotes: 201

Views: 97651

Answers (11)

iwarv
iwarv

Reputation: 451

For those poor souls working in embedded that end up here looking for a similar solution for their IAR compiler (so technically out-of-scope for this question), despair not.

You need to provide compiler option --header_context. When an error occurs in header X while compiling source A, this option will print a neat breadcrumb trail of the all the #includes between both.

I don't use the IAR EW much, but had a quick look. It doesn't look like there is a convenient checkbox for this option. So you will probably have to manually add it in the project settings / "C/C++ Compiler" / "Extra Options".

For cmake, it's a clear choice between

add_compile_options(
    $<$<COMPILE_LANGUAGE:C,CXX>:--header_context>
)
# or 
target_compile_options({your-target-name-here}
    PRIVATE
        $<$<COMPILE_LANGUAGE:C,CXX>:--header_context>
)

Or slight variations thereof.

Upvotes: 1

xtofl
xtofl

Reputation: 41509

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


EDIT (Jan 9 2024)

2022 17.9 will contain a much more useful tool: "#include Diagnostics"; cf. blog post.

Upvotes: 296

Jonathan
Jonathan

Reputation: 7098

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

Rider Include Hierachy

Upvotes: 0

Daniel F. Thornton
Daniel F. Thornton

Reputation: 3685

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.

Upvotes: 3

Colin Desmond
Colin Desmond

Reputation: 4854

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.

Upvotes: 16

Santiago Villafuerte
Santiago Villafuerte

Reputation: 783

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.

Upvotes: 3

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33864

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.

Upvotes: 5

Paul
Paul

Reputation: 463

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.

Upvotes: 7

Agnel Kurian
Agnel Kurian

Reputation: 59466

Try redhat Source-Navigator for a more graphical solution.

Upvotes: 3

Kim Gr&#228;sman
Kim Gr&#228;sman

Reputation: 7586

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.

Upvotes: 25

polyglot
polyglot

Reputation: 2058

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.

Upvotes: 1

Related Questions