Best GCC warning flags for compiling C++

July 18, 2014 [ACCU, C++, Programming, Tech]

A recent discussion on ACCU-general gave people an opportunity to share the warning flags they like to use with g++.

I thought I'd write down the consensus as I understood it, mainly for my own reference:

-Wredundant-decls
-Wcast-align
-Wmissing-declarations
-Wmissing-include-dirs
-Wswitch-enum
-Wswitch-default
-Wextra
-Wall
-Werror
-Winvalid-pch
-Wredundant-decls
-Wmissing-prototypes
-Wformat=2
-Wmissing-format-attribute
-Wformat-nonliteral

We were advised by Jonathan Wakely that -Weffc++ is not very useful since it is mostly based on the first edition of the book Effective C++, many of whose recommendations were improved in the second edition, and also apparently GCC doesn't do a great job of warning about them.

Update: thanks to an article[1] by Roger Orr in CVu these flags are highly recommended in GCC 5.2+:

-flto
-Wodr

[1] Orr, Roger "One Definition Rule", in CVu Volume 27, Issue 5 p16 (editor: Steve Love)

Comments

No Name 2019-05-09

-Wredundant-decls is duplicated

Martin Moene 2014-07-19

Thanks Andy,

Would be nice to have each one linked to the right spot in the documentation [1], but that seems unsupported.

What advised the ordering of this list?

cheers,
Martin

[1] e.g. http://gcc.gnu.org/onlinedocs/gcc/Warning-Opt..., -Wcast-align

Andy Balaam 2014-07-20

Hi Martin, I don’t think the order matters does it? I just copied in from people’s emails in the order I found them…

Martin Moene 2014-07-21

Hi Andy,

The compiler probably cares less :)

-Wall
-Wcast-align
-Werror
-Wextra
-Wformat-nonliteral
-Wformat=2
-Winvalid-pch
-Wmissing-declarations
-Wmissing-format-attribute
-Wmissing-include-dirs
-Wmissing-prototypes
-Wredundant-decls
-Wredundant-decls
-Wswitch-default
-Wswitch-enum