4

So I know I can turn warnings into errors using -Werror=... but I want to make the following warning into an error:

"Class xxx has virtual functions but non-virtual destructor"

The only way I know you can get this error is by turning on the overly obnoxious -Weffc++ flag. Is there a way (or what is the sub-flag in -Weffc++ for this warning) to just print this warning and then turn it into an error?

Thanks!

user869525
  • 769
  • 2
  • 12
  • 21
  • This might help you: http://stackoverflow.com/questions/475407/make-one-gcc-warning-an-error – swalog Jun 08 '12 at 13:08
  • I've seen that post, but wouldn't that mean I would still need to have the -Weffc++ flag on and then in Werror=... I catch the specific warning? I just want to print out the specific warning too. – user869525 Jun 08 '12 at 13:15
  • What is the exact question, how to enable that warning? how to make that warning (and that alone) an error (while allowing for other warnings)? – David Rodríguez - dribeas Jun 08 '12 at 13:25
  • How do I enable that warning without enabling all the other warnings that come along with -Weffc++ and then I want to take that isolated warning and make it into an error. – user869525 Jun 08 '12 at 13:32
  • Ok, the flag is -Wnon-virtual-dtor now I need to turn it into an error. – user869525 Jun 08 '12 at 13:36

1 Answers1

7

-Wnon-virtual-dtor is the name of the specific warning that is turned on by -Weffc++. To turn any warning into an error, you use -Werror=.... So if the warning were -Wspam, making it into an error would be -Werror=spam. So in this case, you would use -Werror=non-virtual-dtor.

However, I don't feel that this warning is especially useful if you are on GCC 4.8 and up. Then you have access to the superior -Wdelete-non-virtual-dtor:

Warn when delete is used to destroy an instance of a class that has virtual functions and non-virtual destructor. It is unsafe to delete an instance of a derived class through a pointer to a base class if the base class does not have a virtual destructor. This warning is enabled by -Wall.

Note that g++ -Wspam -Werror=spam is the same thing as g++ -Werror=spam. Turning a warning into an error automatically turns that warning on.

On a related note, you're not the only one who thinks that -Weffc++ is a little overzealous.

David Stone
  • 26,872
  • 14
  • 68
  • 84
  • Great answer, thank you! I was aware of the -Wdelete-non-virtual-dtor; however, we are confined to using an older version of GCC. – user869525 Jun 08 '12 at 16:59