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

Viewed 7647

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?

2 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.

With GCC (And also Clang) you can disable errors for specific warnings.

Using -Wno-error= followed by the name of the warning (displayed together with the warning or error) will disable the error for that specific item.

For your case with [[deprecated]], use the option -Wno-error=deprecated-declaration, and those will become warnings again instead of errors.

Related