Search code examples
c++gccbuild-processcompiler-warnings

Recommended -W flags for building C++ with gcc


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?


Solution

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