Temporarily Disable Compile Warnings in Visual Studio for Includes

2k Views Asked by At

I'm running Visual Studio 2017. I'm trying to add a Scripting Language called ChaiScript to my project, but it generates A LOT of warnings when I have /Wall on, and I also have treat warnings as errors on (I prefer this to stay like this).

So I figured I would try to temporarily disable all the warnings when including the ChaiScript header file (only need to include 1 file). At first I did this...

#pragma warning( disable : 4061 )
#pragma warning( disable : 4068 )
#pragma warning( disable : 4191 )
#pragma warning( disable : 4355 )
#pragma warning( disable : 4365 )
#pragma warning( disable : 4371 )
#pragma warning( disable : 4464 )
#pragma warning( disable : 4514 )
#pragma warning( disable : 4571 )
#pragma warning( disable : 4623 )
#pragma warning( disable : 4625 )
#pragma warning( disable : 4626 )
#pragma warning( disable : 4668 )
#pragma warning( disable : 4710 )
#pragma warning( disable : 4774 )
#pragma warning( disable : 4820 )
#pragma warning( disable : 5026 )
#pragma warning( disable : 5027 )
#include <chaiscript\chaiscript.hpp>

This works, I can compile... However, I then wanted to have these warnings re-enabled for the rest of the compile process.... So I turned it into this...

#pragma warning( push )
#pragma warning( disable : 4061 )
#pragma warning( disable : 4068 )
#pragma warning( disable : 4191 )
#pragma warning( disable : 4355 )
#pragma warning( disable : 4365 )
#pragma warning( disable : 4371 )
#pragma warning( disable : 4464 )
#pragma warning( disable : 4514 )
#pragma warning( disable : 4571 )
#pragma warning( disable : 4623 )
#pragma warning( disable : 4625 )
#pragma warning( disable : 4626 )
#pragma warning( disable : 4668 )
#pragma warning( disable : 4710 )
#pragma warning( disable : 4774 )
#pragma warning( disable : 4820 )
#pragma warning( disable : 5026 )
#pragma warning( disable : 5027 )
#include <chaiscript\chaiscript.hpp>
#pragma warning( pop )

Now I thought this would make it so it would compile the header without the warnings, then go back to checking for those warnings.... but it seems that after I do this... I still get 4 C4710 warnings... Am I doing something wrong?

https://i.imgur.com/RuxboQC.png

2

There are 2 best solutions below

0
On

Add a #pragma warning(push, 3) before all the disables and a #pragma warning(pop) after the include.

0
On

In this particular case, the problem is likely that, although the warning was disabled when compiling the function definition, it was re-enabled where the compiler had the option of inlining it. There doesn't seem to be a practical solution to that.

Microsoft lists the warnings that are off-by-default. The difference between /W4 and /Wall is that /Wall enables the off-by-default warnings. The off-by-default warnings are off-by-default because they are either low value and/or because you'd likely get many false positives. It might be instructive to occasionally check your project with /Wall to see if there's any useful information in there, but it doesn't seem practical to have them enabled all the time.

I'm a huge fan of pushing the warning level as high as is practical, but /Wall is a step too far for me. I recommend /W4 with /WX everywhere. If necessary for third-party code, drop down to /W3 using #pragma warning(push/pop) as you've shown. In my experience, all the Microsoft headers compile cleanly at /W4.