Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell Visual Studio to treat a warning as a warning, not an error?

In Visual Studio, warning C4996 (using a deprecated function) is treated as an error, and code that uses deprecated functions doesn't compile at all.

There are various ways to disable C4996 entirely, either suppressing it for a single line, for a translation unit, or for an entire project. But what if I want it to still raise a warning, but allow compilation while not treating it as an error?

#include <iostream>

[[deprecated]]
void deprecated_function()
{
    std::cout << "I function, but have been deprecated.\n";
}

int main() {
    deprecated_function();
}

This doesn't compile at all.

#include <iostream>

[[deprecated]]
void deprecated_function()
{
    std::cout << "I function, but have been deprecated.\n";
}

int main() {
#pragma warning(suppress: 4996)
    deprecated_function();
}

This compiles, but doesn't issue a warning at all.

Is it possible to tell Visual Studio to emit a warning, but still allow compilation, for a deprecated function? I'm thinking of refactoring purposes, where I'd like to mark a function as deprecated, identify all the places it's used, but the code still compiles at each intermediate stage where some but not all uses of the deprecated function have been replaced.

I'm compiling using Visual Studio 2019 Community 16.8.4, warning level set to /W3, "Treat Warnings as Errors" set to "No". This particular warning seems to be treated as an error if it gets emitted at all.

like image 940
Nathan Pierson Avatar asked Feb 02 '21 05:02

Nathan Pierson


People also ask

How do I get rid of warning treatment as error in Visual Studio?

Turn off the warning for a project in Visual Studio Select the Configuration Properties > C/C++ > Advanced property page. Edit the Disable Specific Warnings property to add 4996 . Choose OK to apply your changes.

Should I treat warnings as errors?

Yes, even once in a while you will encounter the occasional warning you'd be better off leaving as a warning or even disabling completely. Those should be the exception to the rule, though. Here's some practical advise: at the start of a new project, start treating all warnings as errors, by default.

How do I get a warning in Visual Studio?

The Compile tab of the Project Designer page allows you to turn warnings on and off. Select the Disable All Warnings check box to disable all warnings; select the Treat All Warnings as Errors to treat all warnings as errors.


1 Answers

Found a working solution. It turns out the relevant flag is actually "SDL checks", not "Treat Warnings as Errors". Flipping it from /sdl to /sdl- causes compilation to emit a warning while still compiling.

EDIT: If I want to keep all the SDL checks on except treating Warning C4996 as an error, I can use the flag /sdl in combination with the flag /w34996, which specifies that 4996 is treated as a level 3 warning instead of an error.

like image 167
Nathan Pierson Avatar answered Oct 17 '22 22:10

Nathan Pierson