How to enable the highest warning level in GCC compiler(Boost is heavily used)
Contrary to cl which has 4 levels, gcc only has a set of options that you can turn on or off.
As mentioned by others, the -Wall
is the default, which turns on many warnings already. The -pedantic
option adds a few more. And -Wextra
yet another group...
But to really capture many warnings, you'll have to add many manually.
There is a set I like to use, although someone told me that some of those were contradictory, I find that list rather good for my development work:
-Werror -Wall -Wextra -pedantic -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wundef -Wno-unused -Wno-variadic-macros -Wno-parentheses -fdiagnostics-show-option
Note that I make use of -Werror
because otherwise you get warnings and tend to ignore them. With -Werror
, no more ignoring anything! Write pristine code and your software is much more likely to work as expected.
Related videos on Youtube
Yang
Updated on June 04, 2022Comments
-
Yang almost 2 years
I just read a book which recommends enable the highest warning level in GCC. I just check the doc online, and found there are too much parameters. I want to enable the highest warning level, which parameter should I use?
And we use Boost heavily in our project.
-
chris over 10 years
-Wall -Wextra -pedantic-errors
is a good start, if not everything you need. -
aaronman over 10 yearsjust for anyone using clang on apple it's got
-Weverything
-
-
Alexis Wilke over 10 yearsWith boost you may have to remove a few of the options to not have to turn them off in the code. One thing that a company I'm working with does is to create a set of wrappers for files that create problems. In them you can then use a #pragma to turn off the warnings that would otherwise be generated. For example:
#pragma GCC diagnostic ignored "-Wold-style-cast"
would help if you use a macro that uses a C-like cast. -
JDiMatteo almost 9 years
-Wconversion
is another one you might consider -
rdb almost 6 yearsThis does not even remotely enable all warnings.