Reputation: 1775
int main() {
#pragma warning(push)
#pragma warning(disable: 4101)
int i;
#pragma warning(pop)
}
########################### OR ###########################
int main() {
#pragma warning(suppress: 4101)
int i;
}
I believe either of these should compile on Visual Studio 2013 with warnings treated as errors, with no C4101 warning (about I being an unreferenced local variable.) It's a level 4 warning.
But, it's still giving me the warning. Even if I turn off warnings treated as errors, it still gives me the warning, although then it compiles because there's no error.
I'm not using precompiled headers / stdafx.h. I saw some other questions saying this pragma technique would work around actual code (perhaps they meant code in headers but didn't specify or look like that). Some others vaguely saying without explanation any #pragma commands before stdafx.h would be ignored. But 'https://msdn.microsoft.com/en-us/library/d9x1s805(v=vs.120).aspx' specifically mentions pragma can be in source code to override compiler options. Granted, I've never used stdafx.h, being more a unix guy, so perhaps there's something totally fundamental I'm totally missing.
I've tried specifying C4101 instead of just 4101, and it can't handle an alpha in there. I've tried specifying #pragma warning(push 2) even though that would hit all level 3&4 level warnings which I don't want to do - and even that still gives the level 4 warning.
Here's my compiler command line options: /MP /GS /W4 /Zc:wchar_t /Zi /Gm- /Od /sdl /Fd"<...>\pragmaWarning\Intermediate\vc120.pdb" /fp:precise /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /Fa"<...>\pragmaWarning\Intermediate\" /EHsc /nologo /Fo"<...>\pragmaWarning\Intermediate\" /Fp"<...>\pragmaWarning\Intermediate\pragmaWarning.pch"
As you may see, those command line options omit the warnings treated as errors.
Upvotes: 3
Views: 3606
Reputation: 41047
Some warnings are 'function scope' and cannot be suppressed by using #pragma warning(suppress: 4101)
. They also, as you have noted, cannot be disabled by just doing it around the specific line. You must disable this warning for the whole function:
#pragma warning(push)
#pragma warning(disable: 4101)
int main() {
int i;
}
#pragma warning(pop)
Note also that for warnings that are specific to a line rather than a scope, the use of #pragma warning(suppress: xxx)
is specific to the next actual line in the file.
Upvotes: 9
Reputation: 121599
I agree, #pragma warning(suppress: 4101)
should do it.
Q: Why not try #pragma warning(disable: 4101)
?
Or
#pragma warning(push)
#pragma warning(disable: 4101)
int i;
... more code ...
#pragma warning(pop)
Here's the Microsoft documentation:
https://msdn.microsoft.com/en-us/library/2c8f766e.aspx
Upvotes: 0