Identify which file has included some particular header file - c++

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.

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

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.

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.

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.

Related

Clang doesn't know PTRDIFF_MAX?

I'm upgrading our build machine (from Windows Server 2008 to Windows 10) and have a build that ran fine on the old machine (using Visual Studio). I had some problems getting it to run with VS Build Tools (I'm trying to avoid needing a VS license) on the new machine, so I'm porting it to use Clang.
The build uses Flatbuffers, which I have upgraded from 1.4.0 to 1.12.0. I'm getting the following error during the build:
[2/70] Building CXX object CMakeFiles\polyphemus.dir\hardwareconfig.cpp.obj
FAILED: CMakeFiles/polyphemus.dir/hardwareconfig.cpp.obj
C:\PROGRA~2\MICROS~1\2019\BUILDT~1\VC\Tools\Llvm\x64\bin\clang-cl.exe /nologo -TP -I"C:\Program Files (x86)\nanomsg\include\nanomsg" -I. -I"C:\Program Files (x86)\FlatBuffers\include" -IE:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\include /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /showIncludes /FoCMakeFiles\polyphemus.dir\hardwareconfig.cpp.obj /FdCMakeFiles\polyphemus.dir\polyphemus.pdb -c E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\hardwareconfig.cpp
In file included from E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\hardwareconfig.cpp:3:
In file included from .\hardwareconfig_generated.h:7:
In file included from C:\Program Files (x86)\FlatBuffers\include\flatbuffers/flatbuffers.h:20:
In file included from C:\Program Files (x86)\FlatBuffers\include\flatbuffers/base.h:45:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\algorithm(37,63): error: use of undeclared identifier 'PTRDIFF_MAX'
return static_cast<ptrdiff_t>(_Min_value(static_cast<_CT>(PTRDIFF_MAX), static_cast<_CT>(_Value)));
Not having PTRDIFF_MAX defined is really rather odd, and suggests that Flatbuffers should have done #include <cstdint> in its files, but hasn't. This seems like something that would cause tests to fail, so I can't believe that's an undiagnosed bug. Sure enough, if I add #include <cstdint> to my files, they start building. This really doesn't seem like the correct solution though - I can't believe that Flatbuffers has an undocumented dependency that you have to do that import yourself, which suggests I'm doing something wrong. Does anyone know what, please?
EDIT: Explicitly turning on C++17 in cmake (and from there in clang) results in the same error in a different place that doesn't involve flatbuffers:
In file included from E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\complexseries.cpp:2:
In file included from E:\JenkinsMIDEBLD\workspace\btaylor-new_build_node_CYC-268_2\polyphemus\include\polyphemus/complexseries.h:4:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\complex:12:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\sstream:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\istream:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\ostream:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\ios:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\xlocnum:16:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\streambuf:11:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\xiosbase:12:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\system_error:14:
In file included from C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\stdexcept:12:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.26.28801\include\xstring(1258,47): error: use of undeclared identifier 'PTRDIFF_MAX'
return _Min_value(static_cast<size_t>(PTRDIFF_MAX), static_cast<size_t>(-1) / sizeof(_Elem));
That's in a file where I'd done my own #include of , so I guess that's a red herring (and also not a fix :-/ ).
Turned out that I had in my include path a version of stdint.h that specifies it was for use "only with Microsoft Visual C++ compilers!". Unsurprisingly, it turns out it doesn't work with Clang. You'd expect it to either fail catastrophically or else just work anyway (it does define PTRDIFF_MAX), but I guess it just causes Clang to be very very confused.
Anyway, if I remove it from my include path, things start working.

Visual Studio can't open any C++ header file

I've just started using Microsoft Visual Studio to code with C++, and even when writing a simple code like the following:
#include <iostream>
using std namespace
int main()
{
cout << "Hello world" << endl;
return 0;
}
I end up getting 300 or something errors.
Seems you have not specified the paths to your include and lib directories.
In VS IDE, select Menu /Project/Property/Configuration Properties/VC++ Directories: ( these paths will be slightly different in your case and later you will know how to allow your IDE to include these paths by default to your projects)
Include directories:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\include
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\atlmfc\include
Lib directories:
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\lib\amd64
C:\Program Files (x86)\Microsoft Visual Studio\Shared\14.0\VC\atlmfc\lib\amd64

Update visual studio 2017, now getting compile error C7510: 'Callback': use of dependent template name must be prefixed with 'template'

I tried compiling my project as usual after the update (15.8.0). I set showincludes to yes to figure out where the error originates, but it's all system code. Starting with stdafx.cpp, it goes through all the includes and errors out:
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\poppack.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\poppack.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\winrt\wrl\event.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\winrt\eventtoken.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\shared\pshpack8.h
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\winrt\wrl\event.h(316): error C7510: 'Callback': use of dependent template name must be prefixed with 'template'
1>c:\program files (x86)\windows kits\10\include\10.0.17134.0\winrt\wrl\event.h(324): error C7510: 'Callback': use of dependent template name must be prefixed with 'template'
Has anyone seen this before? I googled up and down to find an answer to no avail. Short of modifying the windows sdk, not sure what to do.
Edit:
In my installed windows SDK, the error was in the file-
C:\Program Files (x86)\Windows Kits\10\Include\10.0.17134.0\winrt\wrl\event.h
Changed line 316: return DelegateHelper::Traits::Callback(Details::Forward(callback));
to: return DelegateHelper::Traits::template Callback(Details::Forward(callback));
and line 324: return DelegateHelper::Traits::Callback(
to return DelegateHelper::Traits::template Callback(Details::Forward(callback));
Since modifying an sdk is not really a solution, Peng Du's solution by selecting non conformance in the configuration window is the way to go.
I have legacy projects and I compared the project settings side by side, finally I successfully built the new project by setting: Configuration Properties > C/C++ > Language > Conformance mode = No
When you use a dependent template name, you have to use a template keyword, for example:
foo.template bar<T>();
Till some moment MSVC was not strict about using typename and template disambiguators, but after an update the rules have changed.
The problem is in the Windows Runtime Library and some change to Visual Studio broke it. I have the same issue on this laptop which is updated to 15.8.2, my machine at home running an earlier version does not do this, since the exact same code compiles on my other machine, it's got to be a bug in VS, or a change required in WRL/event class.
EDIT: Fix to the return values above worked, bug is in the SDK, you should NOT disable /permissive- as this enables protection against Spectre and other security enhancements.

How to find which header is including another header in the include tree?

In the D3D12HelloWindow project from Microsoft DirectX samples there is a reference to IID_PPV_ARGS. When I navigate with F12 in VS2017 enterprise, I reach combaseapi.h, but the output from the compiler with /showincludes does not quote it at all.
In the end, the project compiles and run correctly, but I cannot know which header is including directly or undirectly combaseapi.h ?
How can I find which header is using combaseapi.h ?
The goal/problem is to understand a certain implementation (a certain DirectX 12 aspect) so I can use it to enrich my own (in that case a DirectX 12 WPF asset).
I created very small example on my own. It doesn't work but compiling successfully:
#include "stdafx.h"
#include "Objbase.h"
#include "Unknwn.h"
#include "Propsys.h"
int main()
{
IPropertyStore *pPropertyStore;
CoCreateInstance(IID_IUnknown, NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pPropertyStore));
return 0;
}
In Visual Studio 2017, I see combaseapi.h included through Objbase.h header:
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\Objbase.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\winapifamily.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\winpackagefamily.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\rpc.h
...
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\ole2.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\objbase.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\shared\pshpack8.h
1>Note: including file: C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um\combaseapi.h
...
Above compiler Output should be read in following way. If the line below has more indent it means that header in above line is including header from below line. I am including #include "Objbase.h" and in my .cpp file and going through increasing indentation I see combaseapi.h (last line in attached part of Output). From this I know that Objbase.h through 5 headers through his way is including combaseapi.h.
I am using Windows 10.
I don't see any remarks in Microsoft documentation
Maybe try to check debugger Output again?

visual studio 2015 doesn't see the header

I'm trying to compile project connected with drivers, but VS doesn't see the header files ntddk.h and vadefs.h. I've found both of them located in:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\km
added them in projects properties:
but it stil doesn't work..
You've got a $ before the path, so it's getting confused and treating it as if you want a macro called $C:\Program which you don't have. Just use this:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\km\;%(AdditionalIncludeDirectories)