40

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,738
  • 21
  • 105
  • 131
  • 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
  • The follow-up question is *[Change some GCC warnings into errors](https://stackoverflow.com/questions/479017/)* (two days later). – Peter Mortensen Oct 22 '22 at 02:43

5 Answers5

34

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,738
  • 21
  • 105
  • 131
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
16

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

  • "`-Werror-implicit-function-declaration`" seems to [have been replaced](https://github.com/Barro/compiler-warnings/blob/master/gcc/warnings-gcc-4.6.txt) by "`-Werror=implicit-function-declaration`" in GCC 4.6 (GCC 4.6.0 [was released](https://gcc.gnu.org/releases.html) 2011-03-25). [GCC 4.5 still had it](https://github.com/Barro/compiler-warnings/blob/master/gcc/warnings-gcc-4.5.txt) (GCC 4.5.0 was released 2010-04-14). Or in other words, "-Werror-implicit-function-declaration" can not be found in the current documentation... – Peter Mortensen Oct 26 '22 at 23:03
  • But neither can "[-Werror](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Werror)=implicit-function-declaration" as it is not explicitly listed... – Peter Mortensen Oct 26 '22 at 23:13
  • [Documentation for `-Wimplicit-function-declaration`](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wimplicit-function-declaration) (covered by the documentation for `-Wno-implicit-function-declaration`...). Why does the documentation for GCC have to be so confusing and difficult to navigate? – Peter Mortensen Oct 26 '22 at 23:15
  • Or is it an alias so `-Werror-implicit-function-declaration` can still be used? – Peter Mortensen Oct 26 '22 at 23:23
8

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,738
  • 21
  • 105
  • 131
Stephen Doyle
  • 3,734
  • 1
  • 23
  • 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
  • @Ruslan, maintaining warning free code is good practice when developing. Warnings often point to real bugs. If your code is full of benign warnings you wont notice new ones that may be important. However, in production you should disable -Werror because when other people build your code warnings should not cause the build to fail. So for the love of robust code, do use -Werror! And don't believe everything someone puts in a blog post or a stackoverflow comment for that matter. – jcoffland Nov 18 '22 at 13:01
  • @jcoffland if I cite someone's blog it does mean that I agree with the blogger on the topic. I don't cite blog posts of random people just because I believe everything I find. And concerning the warnings, they should be used wisely, not just to please any compiler you find. There are often quite useless, and sometimes even harmful warnings in the sense that "fixing" them both harms readability and doesn't improve the code. – Ruslan Nov 18 '22 at 15:55
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
strager
  • 88,763
  • 26
  • 134
  • 176
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,738
  • 21
  • 105
  • 131
Jonathan
  • 76
  • 2