Search code examples
c++gccboostcompiler-constructionwarnings

How to enable the highest warning level in GCC compiler(Boost is heavily used)


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.


Solution

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


    Using libraries that have warnings

    I don't currently have a good example for boost, however, I have one for ImageMagick. Many libraries are offered for C and do not really test much in C++ or the developers do not believe in the fact that the compiler can help them find real bugs so they don't turn on warnings.

    As a result, we often have such in headers. Here is how I include ImageMagick headers in my C++ files:

    // ImageMagick
    //
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wunused-parameter"
    #pragma GCC diagnostic ignored "-Wredundant-decls"
    #pragma GCC diagnostic ignored "-Wold-style-cast"
    #include    <Magick++.h>
    #pragma GCC diagnostic pop
    

    This allows me to have all of those warnings in effect and still use libraries that have warning issues.

    Note that the push + pop are used to keep the warnings the way they are defined on the command line. At times, though that is not working properly (i.e. the warning may be discovered after you #include ... headers, in that case, you need to move the diagnostic pragmas to the location where the warning appears).