12

Sometimes with a complex header structure it happens some header is included, but it is hard to tell where from.

Is there some tool (depedency viewer?) or a method how to find the "inclusion stack" (which source / which header / which header / ...) is including one particular header file?

If the header file is included multiple times, finding first inclusion is sufficient, finding all inclusions is a welcome bonus.

Suma
  • 33,181
  • 16
  • 123
  • 191
  • grep is not enough, because of indirect inclusions (this is what I meant by a complex header structure, perhaps I should be clearer). – Suma Aug 09 '10 at 09:17
  • 1
    Or use a cross-referencer (a.k.a. 'grep on steroids') such as [OpenGrok](http://hub.opensolaris.org/bin/view/Project+opengrok/). Although that still won't help you find indirect inclusions. – Gareth Stockwell Aug 09 '10 at 09:18
  • 3
    [Doxygen](http://www.stack.nl/~dimitri/doxygen/) can produce include graphs, but it can be pretty slow to parse very large codebases. – Gareth Stockwell Aug 09 '10 at 09:19
  • @Neil: grep won't help with headers that `#include` other headers. And if there is conditional inclusion, calling grep again on the results of the first grep will not give useful results. – Gilles 'SO- stop being evil' Aug 09 '10 at 09:33

4 Answers4

13

Someone has posted about it but I can't find this answer. So, In VS, go to your project properties. Choose Configuration Properties / C/C++ / Advanced / Show Includes and set "yes".

then compile you cpp file. It looks like this: cpp file:

#include <stdio.h>

int main()
{
    return 0;
}

In the output window after compiling you will see:

1>------ Build started: Project: stlport_project, Configuration: Release Win32 ------
1>Compiling...
1>stlport_project.cpp
1>Note: including file: D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stdio.h
1>Note: including file:  D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stl/_prolog.h
1>Note: including file:   D:\src\hrs_rt_059.00\HRS\modules\src\libs\src\external\stlport\5.1.7\stl/config/features.h

and so on

EDIT: reference to the same question Displaying the #include hierarchy for a C++ file in Visual Studio

maxstrobel
  • 497
  • 4
  • 17
8

The header you are searching for may not be directly included into the source file. You need to 'preprocess_only' the code. This can be done in g++ by using the -E option; I don't know enough about visual C to know what the exact specification is there but if you look in the help for 'preprocess' you may come up with something.

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
6

A somewhat hacky approach (but one which should work on any platform/toolchain, without needing a separate dependency analyser) is simply to add a #error at the top of the included header - you will then get a compilation error from the first .cpp file which includes it.

Gareth Stockwell
  • 3,112
  • 18
  • 23
  • Nice and "portable" (no special tools needed) idea. A drawback is some headers (e.g. standard libraries) may be read only for you, or touching them can cause excessive rebuild as a result of them being frequently used. – Suma Aug 09 '10 at 09:09
  • 1
    @Suma: if you want to avoid modifying a standard header, put your own temporary copy with `#error` added early on the include path. – Gilles 'SO- stop being evil' Aug 09 '10 at 09:32
1

Visual Studio /showIncludes

Directly in the Visual Studio I have found an option called /showIncludes - the output is textual only, but indented in a way which makes reading it quite easy:

Note: including file: /*..path.anonymized..*/\TCMalloc\windows\config.h
Note: including file:  /*..path.anonymized..*/\memalloc\tcmalloc\windows/port.h
Note: including file:   C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\windows.h
Note: including file:    C:\Program Files\Microsoft Visual Studio 8\VC\include\excpt.h
Note: including file:     C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h
Note: including file:      C:\Program Files\Microsoft Visual Studio 8\VC\include\sal.h
Note: including file:      C:\Program Files\Microsoft Visual Studio 8\VC\include\vadefs.h
Note: including file:    C:\Program Files\Microsoft Visual Studio 8\VC\include\stdarg.h
Note: including file:    C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\windef.h
Note: including file:     C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h
Note: including file:      C:\Program Files\Microsoft Visual Studio 8\VC\include\ctype.h
Note: including file:       C:\Program Files\Microsoft Visual Studio 8\VC\include\crtdefs.h

ProFactor Include Manager

There is also a VS add-in called Include Manager which seems to provide the needed functionality in a very nice visual way.

Suma
  • 33,181
  • 16
  • 123
  • 191