Suppress warnings for external headers in VS2017 Code Analysis - c++

I want to use the Code Analysis in Visual Studio 2017 but I'm using Qt and it gives me a lot of warnings from the headers. I've tried turning off warnings:
#pragma warning(push, 0)
#include <QtGlobal>
#pragma warning(pop)
but it doesn't help. I also tried using this:
#include <codeanalysis\warnings.h>
#pragma warning(push, 0)
#pragma warning(disable : ALL_CODE_ANALYSIS_WARNINGS)
#include <QtGlobal>
#pragma warning(pop)
but no help. How can I disable the Code Analysis for the Qt external headers?

If you open your .vcxproj file, down the bottom you should see:
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
below this you can add:
<PropertyGroup Condition="'$(Language)'=='C++'">
<IncludePath>$(QTDIR)\include;.\GeneratedFiles;$(IncludePath)</IncludePath>
<CAExcludePath>$(QTDIR)\include;.\GeneratedFiles;$(CAExcludePath)</CAExcludePath>
</PropertyGroup>
Microsoft say there's a bug where CAExcludePath is overwritten by IncludePath but this is fixed in Visual Studio 2017 V15.3 and you'll only need to set CAExcludePath - I haven't verified this (I'll update this once I do).
This answer came from How can I suppress warnings for external headers in VS2017 Code Analysis?

Related

Why does #pragma warning(push,0) enable additional warnings in MSVC 2015?

MCVE:
#pragma warning(push,0)
#include <QWidget>
#pragma warning(pop)
#include <QVariant>
int main(int argc, char *argv[])
{
}
compiling with Visual Studio 2015 (choose "Qt Application" as project type if it matters) and /W4 i get 2xWarning C4100 (unreferenced formal parameter) for argc and argv which is what i expect and are actually interested in. But in addition i get 5xWarning C4251(class X needs to have dll-interface to be used by clients of struct Y) somewhere inside qvariant.h.
With different included files from Qt i can produce different warnings, so it's not just C4251. I was not able to reproduce it with standard library includes yet. The warnings disappear if i remove/comment the #pragma warning-lines or set the projects /W flag to a lower level. The level pushed seems to have no effect, so e.g. with /W0 and #pragma warning(push,4) i get no warnings.
What i would have expected is, that #pragma warning(push,0) disables all warnings inside <QWidget> and #pragma warning(pop) restores the initial state. Instead, Visual Studio seems to somehow detect that those files are from an external library (or is it something inside the Qt code?), but as soon as there is a push/pop pair the pushed warning-level is ignored and instead the initially set warning level via /W flag is used.
I came across this problem initially when using CMake and cotire, which automatically generates a header file to be used as precompiled header. So in my code i don't use #pragma warning (push/pop) which compiles fine without Qt-Warnings. But the generated file wraps the includes in a push/pop pair and thus generates about 200 Warnings in my project. I'm trying to understand why this happens in order to find a solution which requires me neither to wrap all my manual includes in push/pop nor to maintain a private modification to cotire.

ShGetKnownFolderPath not working C++ [duplicate]

(Visual Studio 2010 / Visual C++ / Windows 7)
example.cpp:
#include <Shlobj.h>
#pragma comment (lib, "Shell32.lib")
...
void example()
{
SHGetKnownFolderPath(...) // undefined
}
I'm doing everything according to documentation and what I see in other threads, but it still doesn't work.
I had exactly the same problem. Another project with the same code and ancillary files (but different includes) was working.
Putting #include <Shlobj.h> at the top of the file solved the problem.
It might not be replicable though, as it should have worked without doing that. Probably another Visual Studio bug.
Try putting following statement before all includes:
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
Since the documentation says it needs Vista/2008 minimum.

Visual Studio 2015 won't suppress error C4996

Visual Studio 2015 Community Edition gives the following error when compiling in debug, but not when compiling in release:
std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
I was able to trace back the source of this error to lines 214 and 242 of this third party library I'm using to write bitmap images. I don't fully understand what is going on in these parts, so I'd rather not mess with it.
I'm trying to disable this error, but Visual Studio won't let me. I've tried the following solutions that were suggested in the documentation, on StackOverflow, or elsewhere:
Add 4996 to the "Disable Specific Warnings" field in Project Settings > Configuration Properties > C/C++ > Advanced.
Add /wd4996 to the "Command Arguments" field in Project Settings > Configuration Properties > Debugging.
Add #pragma warning (disable : 4996) at the top of the offending file, and/or above the offending function.
Add _SCL_SECURE_NO_WARNINGS, _SCL_NONSTDC_NO_WARNINGS, _SCL_OBSOLETE_NO_WARNINGS, _SCL_SECURE_NO_WARNINGS_GLOBAL, and combinations thereof to the "Preprocessor Definitions" field in Project Settings > Configuration Properties > C/C++ > Preprocessor.
Add the definitions from the previous solution with a #define directive to the top of the offending file.
Add the definitions from the previous solution but prefixed with /D or with -D to the "Command Arguments" field.
But none of this fixes the issue for me.
What could possibly be the reason for Visual Studio to keep insisting on displaying this error?
define NO_WARN_MBCS_MFC_DEPRECATION
Disabling warning 4996 has no effect on std::copy warnings. To suppress this warning place the following at the top of your source file:
#define _SECURE_SCL_DEPRECATE 0
#include <algorithm>
Add _CRT_NONSTDC_NO_WARNINGS to preprocessor definitions.
In your stdafx.h:
#pragma warning( push )
#pragma warning( disable: 4996)
#include <algorithm>
#pragma warning( pop )
Worked for me VS2015 update 3

error "Cannot open include file", but it's not included in code

I got the error after installing November CTP in Visual Studio 2012.
The error is in unordered_set.hpp of boost library. But I have not included unordered_set or initializer_list anywhere in the code. I used boost only in one file and I am sure it's not the reason.
error in unordered_set.hpp:
#if !defined(BOOST_NO_0X_HDR_INITIALIZER_LIST)
#include <initializer_list> //error C1083: Cannot open include file: 'initializer_list': No such file or directory d:\boost\unordered\unordered_set.hpp
#endif
EDIT:
I uninstalled November CTP after, but still got the same error.
C++11 is enabled by default in VS2012.
From boost headers
#if !defined(BOOST_NO_0X_HDR_INITIALIZER_LIST)
#include <initializer_list>
#endif
You may try to define BOOST_NO_0X_HDR_INITIALIZER_LIST, or find initializer_list header location.
// config/suffix.hpp
#if defined(BOOST_NO_INITIALIZER_LISTS) && !defined(BOOST_NO_0X_HDR_INITIALIZER_LIST)
# define BOOST_NO_0X_HDR_INITIALIZER_LIST
#endif
// config/compilers/visualc.hpp
#if _MSC_VER <= 1500 || !defined(BOOST_STRICT_CONFIG) // 1500 == VC++ 9.0
# define BOOST_NO_INITIALIZER_LISTS
#endif
BOOST_STRICT_CONFIG may be commented in config/user.hpp.
However, it looks like you have no modern Visual C++ headers, that is strange since you have VS2012 (initializer_list header will be added in VS2010 actually).
Today had the same issue, I found that installing the November CTP is not enough, you need to change the platform toolset under project configuration to Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012) to actually use the new features, hope this helps anyone.

What is the best solution for suppressing warning from a MS include (C4201 in mmsystem.h)

I am tired of having to look at warnings during our compilations - warnings that come from MS include files.
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\mmsystem.h(1840): warning C4201: nonstandard extension used : nameless struct/union"
I have seen this thread that suggests changing the header itself (but then each of my team mates has to do it and we have to do it on the build server - not to mention it is a glorious HACK)
Is there a better way? I don't want to turn it off globally - just to suppress it for certain files or directories.
Any suggestions?
EDIT
For some stupid reason I didn't think I could set warning levels across include files. Thanks - that does the trick.
Something like
#pragma warning(push, disable: 4201)
#include <mmsystem.h>
#pragma warning(pop)
How about using #pragma warning extension in VC++?
http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
#pragma warning (push, 2) // Temporarily setting warning level 2
#include <mmsystem.h>
#pragma warning (pop) // Restore back
You may also try ..
#pragma warning (disable: 4201)
#include <mmsystem.h>
#pragma warning (default)
With VS 2017 Community, to avoid warning C4083, I use :
#pragma warning(push)
#pragma warning(disable:4201)
#include <dxvahd.h>
#pragma warning(pop)