36

I get this warning from GCC:

warning: cannot pass objects of non-POD type 'class Something' through '...'; call will abort at runtime

It's pretty deadly, especially since it calls an abort. Why isn't this an error? I would like to make it an error, but:

  1. How do I make a specific warning an error?
  2. Which warning is it? According to 3.8 Options to Request or Suppress Warnings, -Wno-invalid-offsetof, it looks like the flag to hide it, but it doesn't.
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
  • 4
    Fortunately, modern versions of GCC (at least 4.6.3, but probably earlier) let you know which warning flag triggered a particular warning. For example: `main.cpp:12:15: error: division by zero [-Werror=div-by-zero]` – David Stone Jun 08 '12 at 15:56
  • And that is just such a wonderful feature. – hlovdal Oct 16 '12 at 09:33

5 Answers5

29

I'm not sure what the correct warning is, but once you've found it, you can change its disposition with the following (using 'format' as the example):

#pragma GCC diagnostic error "-Wformat"

Or as strager points out:

gcc -Werror=format ...

I've checked the gcc source for this and this specific warning cannot be disabled via command line flags.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Sean Bright
  • 114,945
  • 17
  • 134
  • 143
15

-Werror=specific-warning will turn the specified -Wspecific-warning into an error in GCC 4.3.x or newer. In 4.1.2, only -Werror-implicit-function-declaration works. Note the hyphen instead of equals sign -- it works for that specific case only and no others. This is one of the more serious common warnings and it's definitely handy to make it into an error.

Apart from that, older versions of GCC only seem to provide the -Werror sledgehammer of making every last warning an error.

7

It sounds like there are a bunch of other warnings that you don't want to be turned into errors (using the -Werror flag). In general, it's good practice to fix all warnings. Using -Werror forces this.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Stephen Doyle
  • 3,714
  • 1
  • 22
  • 18
  • Amen, your code should be warning-free with at least -Wall (although some of the stuff that -Wextra reports are excusable, e.g. unused variables/parameters). – Adam Rosenfield Jan 24 '09 at 03:41
  • 1
    You can do -Wall -Wextra -Wno-unused-parameters -Wno-unused-functions ... Any specific warning can be disabled with the -Wno-$foo syntax, which makes it easy to excuse the specifics. Unfortunately, it doesn't help with any subset of the -pedantic warnings. – Tom Jan 24 '09 at 03:54
  • Warnings are not errors to be "fixed". And warning-free code is an elusive concept, unless you fix compiler version. See also [Tidbits: For the love of god, don't use -Werror!](http://blog.schmorp.de/2016-02-27-tidbits-for-the-love-of-god-dont-use-werror.html). – Ruslan Jan 10 '18 at 09:50
5

You can use the -Werror compiler flag to turn all or some warnings into errors.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
strager
  • 86,676
  • 24
  • 133
  • 174
2

You can use -fdiagnostics-show-option to see the -W option that applies to a particular warning.

Unfortunately, in this case there isn't any specific option that covers that warning.

It appears that there will be better support for this in GCC 4.5.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Jonathan
  • 76
  • 2