Finding how a header file gets included
6 May, 2010 § 2 Comments
Have you ever had a header file generate warnings that is not explicitly part of your Visual Studio solution?
Recently I’ve been working on fixing static analysis warnings reported by Microsoft’s PREfast static analysis tool for C++. I ran in to an issue though with some of the Microsoft Windows SDK v6.0 headers. One file, aptly named `
strsafe.h`, can be the cause for tons of warnings in a build.
I looked around and found that the Windows SDK team is fully aware of the problem. They recommend wrapping the include for
strsafe.h with pragmas, like so:
#include <CodeAnalysis/warnings.h> #pragma warning(push) #pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS) #include <strsafe.h> #pragma warning(pop)
So the first thing I did was to search through my code and find out who what included
strsafe.h. But it turns out that no file explicitly included it. I figured that it must get included from another include file.
To find out what files are included in your build, you can turn on
/showIncludes through the
Project Properties ➨ Configuration Properties ➨ C/C++ ➨ Advanced ➨ Show Includes
/showIncludes prints out a tree of all of the files that get included in when the project is built. Using this, I was able to find which SDK files we included that ended up including
This process took me about a half hour in total from turning the compiler flag on, performing a rebuild, narrowing down the includes, and wrapping them with pragmas. I still got a nasty taste in my mouth for suppressing the warnings, but getting rid of them cut out a lot of noise surrounding actual warnings that are more pressing.
Someone knows if it is possible to enable the same thing using a #pragma definition? Having the option in the property dialog is nice, but it would be nicer to quickly edit your file by adding and removing the #pragma.
In some sense that could be useful, but it wouldn’t have helped my situation. I knew that a file was being included, but couldn’t tell which file was including it out of the 200 or so files in the solution.
This specific #pragma ability does exist, although not by using a #pragma. Just right-click on a .cpp file in Solution Explorer and choose Properties. Within there, you can enable /showIncludes just for that one file. When the project is building, only that file will have its included files printed to the Output window.