I'm reading the gcc manual at the moment, especially the part about warning/error flags. After reading the part about the -Wextra flag, I wonder if it is useful at all. It seems that it complains about things which seem to be rather subjective or a matter of taste. I'm not that experienced with gcc, I only use it from time to time for some small projects at university, so to all experienced C/C++ (or for whatever language you use gcc), what's the deal with -Wextra?
3 Answers
-Wextra
, among other stuff implies -Wtype-limits
:
Warn if a comparison is always true or always false due to the limited range of the data type, but do not warn for constant expressions. For example, warn if an unsigned variable is compared against zero with '<' or '>='. This warning is also enabled by -Wextra.
I find this really useful.
-
But it can also cause annoying false positives, e.g. https://stackoverflow.com/questions/46508831/is-the-max-value-of-size-t-size-max-defined-relative-to-the-other-integer-type, when the comparison might be needed on other platforms. There seems to be no way to prohibit this warning other than hiding the comparison using preprocessor conditions, which is somewhat ugly. – dpi Jun 13 '18 at 14:57
I usually add -Wno-sign-compare
and -Wno-unused-parameter
to remove noise.
The implied -Wuninitialized
(with the -O2
option) has been very helpful to me, but adding it initially to your code base can be a bit daunting. One way to deal with this is to add two macros (which look slightly strange: the equal sign is intentional):
#define ELIMINATE_GCC_WARNING = 0 // used to remove nuisance warnings
#define UNCHECKED_GCC_WARNING = 0 // not yet verified
Then you can quickly eliminate the gcc warnings and get a clean compile by using e.g.:
int foo UNCHECKED_GCC_WARNING;
Then as an optional step go back and check these additions, and change them one-by-one to ELIMINATE_GCC_WARNING. This can be slow. But I would be surprised if you didn't find some existing bugs.

- 9,553
- 10
- 54
- 77
-
3I find `-Wno-unused-parameter` helpful especially when old source code changes over years, sometimes values end up being passed down nested functions which are not even used. Suggest define UNUSED. `#define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))` Then you can wrap arguments like this... `void func(int a, int UNUSED(b)) { ... }` Its also nice that if the argument needs to be used later on GCC will not let this happen without removing the UNUSED() attribute. – ideasman42 Mar 27 '13 at 22:27
-
1Thanks, useful advice, but personally I wouldn't disable -Wsign-compare unless the code base had too many of these to deal with. Because many bugs can arise from comparing signed with unsigned, unless you are certain the int will never be negative and the uint will never be greater than MAX_INT – EmbeddedSoftwareEngineer Mar 24 '21 at 10:20
-
ideasman42 you can also use "(void) paramName;" in the function body to suppress the warning instead of using attributes – EmbeddedSoftwareEngineer Mar 24 '21 at 10:24
Please see http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html.
Therefore it is not only useful for 'always true' comparison (-Wsign-compare) but identifying other compilation issues as well. I find it useful often.

- 1,429
- 1
- 17
- 28