16

I was looking for a list of recommended g++ warning options for C++ and only could find this: Recommended gcc warning options for C and Useful GCC flags for C which are all quite C specific

-Wall and -Wextra enable most but not all of the warnings gcc can generate.

Which warnings that aren't enabled by those options especially when compiling C++ should be turned on as well?

Community
  • 1
  • 1
pmr
  • 58,701
  • 10
  • 113
  • 156

2 Answers2

22

-Wall -Wextra tends to cover the really noteworthy ones. Personally, I also like to compile with -ansi -pedantic and occasionally -Wshadow.

Also, it can be a little noisy and not useful 100% of the time, but -Weffc++ sometimes has good suggestions for better code quality as well.

EDIT In the age of modern C++, you should replace -ansi -pedantic with -std=c++14 -pedantic or whatever your version of choice is, since -ansi will put the compiler into C++98/C++-03 mode.

Evan Teran
  • 87,561
  • 32
  • 179
  • 238
  • 1
    Never knew about effc++. Looks interesting. – pmr Mar 12 '11 at 16:57
  • @pmr you should get the books too! – stefan Mar 12 '11 at 17:10
  • @stefan What makes you think I don't already have them? It's just the compiler switch that surprised me :) – pmr Mar 12 '11 at 17:33
  • @pmr Awesome books none the less. – stefan Mar 12 '11 at 18:09
  • 1
    Unfortunately, -Weffc++ is not well designed/maintained and it reports too much useless stuff. Sadly, I had to disable it. – Offirmo Aug 21 '12 at 13:54
  • 1
    @Offirmo: yea, I agree, that's why i said that it can be noisy and not always useful. – Evan Teran Aug 21 '12 at 15:18
  • @Offirmo do you have specific complaints in mind? It may be possible to improve the warning (I'm not yet volunteering, just trying to understand the opportunities). I've personally been hit by "XXX should be initialized in the member initialization list" for detaul-initialized members which is plain silly. – yugr Jan 05 '22 at 08:06
3

Don't forget -Wstrict-aliasing.

I found this post was good, look the params up: Recommended gcc warning options for C

yugr
  • 19,769
  • 3
  • 51
  • 96
stefan
  • 2,886
  • 21
  • 27