Visual Studio plugin to find out which files are being included implicitly - c++

A particular header file may only be including "Foo.h" but Foo.h is including other headers which implicitly include many other headers. I would like to know all the headers that a particular file is including.
In case anybody is wondering why a plugin for Visual Studio; simply because of the way the include directories are set-up in the project. If an external tool does the job and allows me to specify the locations where it can search for the header files, that will do as well.

I don't know of any plugins that do this, but you could whip up a homebrew solution by turning on the showIncludes flag in your projects settings, then doing a full build and parsing the output. The indentation changes based on the nesting of the includes.

Try out Boost.Wave. It is straight forward to dump all the headers included. They also have an example named list_includes that does exactly that.

Related

VSCode - intellisense ignores headers injected using clang.cxxflags

For various ugly reasons I won't go into here, I need to use the -include compiler flag to inject a header file before compilation. VSCode (Version 1.14.2, running on Ubuntu) does not seem to scan headers injected this way. I have tried adding the option and path to settings.json (clang.cxxflags), but no luck.
Here's the question:
Is there another way to get Intellisense to scan a header without including that header directly in the source file? I've looked at c_cpp_properties.json and see where to add header search paths, but no way to inject specific headers. Obviously, including the header explicitly fixes Intellisense for the file, but that is not an option.
Any feedback is appreciated.

could you remove stdafx.h in visual studio when coding in c++?

i just started learning c++ lately and i am using visual studio 2015. But i have to manually remove the #include stdafx.h in my cpp file before upload it to the test server for my class, or it just sends back an error like this:the error from test server
I would like to know why this issue is happening. My guess is that this thing is exclusive to windows and x86 based OS?
Any help would be appreciated. :D
You'll need to upload all your sources. stdafx.h isn't special in that regard. Of course, if your course restricts you to one .cpp upload, then you can't use stdafx.h for that course
stdafx.h is the default name of a precompiled header for a Visual Studio project. However, you can certainly have a file named this on other environments. Note that the Visual Studio compiler processes this header slightly differently than it does other headers, if it is specified as the precompiled header in your project's property pages (C/C++ -> Precompiled Headers - see here for more details). For example, multiple includes of this file with different preprocessor defines will be ignored when it is the precompiled header (which isn't that common, but something to be aware of).
If you want to make portability to non-Visual Studio environment easier, it's usually easier to disable precompiled headers in your Visual Studio project. If you continue to use the stdafx.h file in your code, you will obviously need to upload it to your test compilation server, and may need to modify it and/or its inclusion into your code.

C++ header files intermittently not found

I have noticed some odd behaviour in VS2012 while working with some C++ projects. If I put the following line at the top of a header file: (myclass.h)
#include <D3DX11.h>
The compiler will tell me that it can't find the file (even though intellisense suggests it). However, if I put the same line in a different file (myclass.cpp) everything works just fine. I don't understand why it can be found when used in one file, but not the other? What is going on ?
EDIT / NOTE:
This isn't really a DirectX specific issue. That is just what I was working with when I decided to ask. This can happen in other places as well.
In the newer versions of visual studio the include paths are set at project-level. Maybe the project containing the cpp has the proper path set (see the comment from Jesse Good in your question). However that might not be the case for another project in your solution. As soon as you put the include in the header, all other projects including your header will need to know where to find the directX-headers as well.
Beware of intellisense - if you open a header it has to guess what cpp you might include this header from and the displayed info is not always correct. I think in the newer versions it even depends on what other files are opened / project is selected.
The compiler should inform you what cpp actually causes problems when including the header.

How does visual studio know which cpp files to rebuild when an include file is changed?

In some of my VS 2005 projects, when I change an include file some of the cpp files are not rebuilt, even though they have a simple #include line in them.
Is this a known bug, or something strange about the projects? Is there any information about how VS works out the dependencies and can I view the files for that?
btw I did try some googling but couldn't find anything about this. I probably need the right search term...
I've experienced this problem from time to time, and with other IDEs too, not just VS. It seems thatv their internal dependency tree sometimes gets out of whack with reality. In these cases, I've found deleting pre-compiled headers (this is important) and doing a complete rebuild always solves the problem. Luckily, it doesn't happen often.
To be honest I never faced such a problem using Visual Studio. Your CPP should be rebuild as well if it includes the header. The only reason I can come up with: same include file is taken from 2 different sources.
You can try do debug this at compile time, by enabling the preprocessor to output preprocessed files. Click on the CPP file go to properties and then to C/C++->Preprocessor and select in "Generate Preprocessed File" the item with or without line numbers.
Go to you include file put the pragmas around your newly added definitions like:
#pragma starting_definition_X
...
#pragma ending_definition_X
Now compile everything. There will be a newly created file with the same name as CPP but with extension .I (or .i).
Make a search if your pragmas are there. If not, your include come from another place.
If you use pre-compiled headers, you cpp should rebuild. There is also a pragma once statement in MS VC, which parses the include file only once, but that should still recompiler you cpp-file.
Hope that helps,
Ovanes
Do you have the "Minimal rebuild" option turned on?
Visual studio compares the timestamps on the files. So you might want to check that your system clock is set correctly and also that none of the files has a funny timestamp on it. Look at the include files, the cpp files, the pch files and obj files and make sure all the timestamps look reasonable. In particular, make sure none of them are in the future.
Was the .h files added in the project? If not, then vs maybe unable to find out the dependency.
Thanks for all the answers they have helped point me in the right direction.
I have discovered that deleting the idb file and rebuilding will then allow subsequent modifications of .h files to cause the correct .cpp files to be built. However this causes the entire project to be rebuilt which just brings me back to Neil Butterworth's suggestion of doing a full rebuild. I don't think there is much else I can do about it.
As an aside, looking at the bad and good idb files I can see that the cpp file that was not being built is not in the bad idb, whereas it is in the good idb. The header file that is being changed is mentioned several times in both files.
win_pdbx (download) can extract the idb file and moyix has published some information about the streams in these files.
Stream 4 contains the file paths of the cpp files but I have not been able to determine the format.

Visual C++ 'Force Includes' option

I have just come across a Visual C++ option that allows you to force file(s) to be included - this came about when I was looking at some code that was missing a #include "StdAfx.h" on each .cpp file, but was actually doing so via this option.
The option can be found on the Advanced C/C++ Configuration Properties page and equates to the /FI compiler option.
This option could prove really useful but before I rush off and start using it I thought I'd ask if there are any gotchas?
I would say the opposite to litb above if you're using precompiled headers. If you use "stdafx.h" as your precompiled header and have code like this:
#include "afile.h"
#include "stdafx.h"
then you'll be spending an age trying to figure out why "afile.h" isn't being included. When using precompiled headers, all the includes and #defines are ignored up to the "stdafx.h". So, if you force include the "stdafx.h" then the above will never happen and you'll get a project that uses the precompiled option efficiently.
As for litb's comment about finding macros, good IDE's usually have an option to jump to the definition of a symbol, whether it be a #define, function, class, etc.
I would discourage from /FI (MSDN says it's called /FI . Not sure whether i looked at the right page though), simply because people or yourself reading the files don't notice a header is magically included anyway.
You can be sure this will cause much debugging time for someone that wants to figure out where specific macros come from, even though there are no #include lines at the top of the file.
Force includes is also helpful for automatically generated source files. Our system uses a tool that generates many source files but they don't include our pre-compiled header file. With "force includes" compilation of these files is now faster.
Optionally, it would be possible to write a script to insert the #include line in those files after generation and before compilation. But why go to that trouble?
I'd side with litb: don't do the forced includes. Having the code be explicit makes it easier to know what's going on for a new user. It also makes it easier to know what's going on if you ever need to port the code to a new platform.
Note that if the code uses templates, Visual Studio usually can't track down the definitions correctly. Perhaps 2010 will be better, but even VS 2008 is problematic on this front.
I wouldn't use it that often but it has its uses. I have used it to add a header that suppressed some warnings to all cpp files so that I could turn on /W4 or /Wall for the project and not have to edit all of the cpp files to include the warning suppression header first. Once eveything was working I DID go back and edit all the cpp files but for a proof of concept /FI was useful.
Likewise you can use it to force a precompiled header into cpp files in some build configurations but not in all (in case you want to have a build configuration that DOESNT use precompiled headers and that makes sure that each cpp only includes exactly what it needs to). However using #pragma hdrstop is, IMHO, a better way to achieve this.
I've talked about all of this on my blog here: http://www.lenholgate.com/blog/2004/07/fi-stlport-precompiled-headers-warning-level-4-and-pragma-hdrstop.html in a little more detail.
Save this function for when something weird comes up - like if you want to include a header in a generated source file. Even then there are likely better ways.