Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using [[deprecated]] attribute when warnings are errors (-Werror)

I'm migrating to C++14 and keen to adopt its [[deprecated]] functionality, e.g.

#include <string>
#include <iostream>

[[deprecated]]
int f() { return 42; }

int main()
{
  std::cout << f() << std::endl;
}

compiled with

g++ example.cpp -std=c++14 -Werror

and the problem is the deprecated warning is promoted (demoted?) to an error and the build fails.

Obviously using a #pragma to silence the warning completely defeats the point. Is there any way to tell g++ to emit warnings yet exclude specific ones from being treated as errors?

like image 521
virgesmith Avatar asked Sep 14 '18 15:09

virgesmith


People also ask

How do I disable warning treatment as error?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

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.

How do you suppress warnings in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.

What is werror?

-Werror= Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors.


1 Answers

You need to add

-Wno-error=deprecated-declarations

to tell gcc to keep deprecated-declarations as a warning instead of making it an error.

You can add additional

-Wno-error=name_of_warning

if you have additional warnings that you would like to not be treated as errors as well.

like image 107
NathanOliver Avatar answered Oct 19 '22 18:10

NathanOliver