Make one gcc warning an error?

2019-01-22 18:34发布

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 http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, -Wno-invalid-offsetof looks like the flag to hide it, but it doesn't

5条回答
太酷不给撩
2楼-- · 2019-01-22 18:53

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

查看更多
Lonely孤独者°
3楼-- · 2019-01-22 18:58

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

查看更多
成全新的幸福
4楼-- · 2019-01-22 19:04

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

#pragma GCC diagnostic error "-Wformat"

Or as strager points out:

gcc -Werror=format ...

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

查看更多
疯言疯语
5楼-- · 2019-01-22 19:08

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

Unfortunately, in this case there is no specific option that covers that warning.

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

查看更多
放荡不羁爱自由
6楼-- · 2019-01-22 19:12

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, its good practice to fix all warnings. Using -Werror forces this.

查看更多
登录 后发表回答