Search

0.11 — Configuring your compiler: Warning and error levels

When you write your programs, the compiler will check to ensure you’ve followed the rules of the C++ language (assuming you’ve turned off compiler extensions, as per lesson 0.10 -- Configuring your compiler: Compiler extensions).

If you have done something that definitively violates the rules of the language, during compilation the compiler will emit an error, providing both line number containing the error, and some text about what was expected vs what was found. The actual error may be on that line, or on a preceding line. Once you’ve identified and fixed the erroneous line(s) of code, you can try compiling again.

In other cases, the compiler may find code that seems like it might be in error, but the compiler can’t be sure (remember the motto: “trust the programmer”). In such cases, the compiler may opt to issue a warning. Warnings do not halt compilation, but are notices to the programmer that something seems amiss.

Best practice

Don’t let warnings pile up. Resolve them as you encounter them (as if they were errors).

In most cases, warnings can be resolved either by fixing the error the warning is pointing out, or by rewriting the line of code generating the warning in such a way that the warning is no longer generated.

In rare cases, it may be necessary to explicitly tell the compiler to not generate a particular warning for the line of code in question. C++ does not support an official way to do this, but many individual compilers (including Visual Studio and GCC) offer solutions (via non-portable #pragma directives) to temporarily disable warnings.

By default, most compilers will only generate warnings about the most obvious issues. However, you can request your compiler be more assertive about providing warnings for things it finds strange.

Best practice

Turn your warning levels up to the maximum, especially while you are learning. It will help you identify possible issues.

Increasing your warning levels

For Visual Studio users

To increase your warning levels, right click on your project name in the Solution Explorer window, then choose Properties:

Solution Explorer Properties

From the Project dialog, first make sure the Configuration field is set to All Configurations.

Then select C/C++ > General tab and set Warning level to Level4 (/W4):

Enable Level 4 Warnings

Note: Do not choose EnableAllWarnings (/Wall) or you will be buried in warnings generated by the C++ standard library.

For Code::Blocks users

From Settings menu > Compiler >Compiler settings tab, find and check the options that correlate with -Wall, -Weffc++, and -Wextra:

Enable All Warnings

Then go to the Other compiler options tab, and add -Wsign-conversion to the text edit area:

Add -Wsign-conversion

Note: The -Werror parameter is explained below.

For GCC/G++ users

Add the following flags to your command line: -Wall -Weffc++ -Wextra -Wsign-conversion

Treat warnings as errors

It is also possible to tell your compiler to treat all warnings as if they were errors (in which case, the compiler will halt compilation if it finds any warnings). This is a good way to enforce the recommendation that you should fix all warnings (if you lack self-discipline, which most of us do).

For Visual Studio users

To treat warnings as errors, right click on your project name in the Solution Explorer window, then choose Properties:

Solution Explorer Properties

From the Project dialog, first make sure the Configuration field is set to All Configurations.

Then select C/C++ > General tab and set Treat Warnings As Errors to Yes (/WX).

Treat warnings as errors

For Code::Blocks users

From Settings menu > Compiler > Other compiler options tab, add -Werror to the text edit area:

Add -Werror

For GCC/G++ users

Add the following flag to your command line: -Werror


1.1 -- Statements and the structure of a program
Index
0.10 -- Configuring your compiler: Compiler extensions

24 comments to 0.11 — Configuring your compiler: Warning and error levels

  • JS

    People who use the command line for compiling can create a shortcut like this. In your .bashrc or .bash_profile (found in the home directory), add following

    Now you can use

    to compile your program.

  • Ryan

    Hi, when I use

    , I get the error:

    . Am I doing this right?

    • You need to tell g++ which files you want to compile

  • Zaki

    Hello sir, I have 2 questions:
    1. What is the function of -fexecptions flags when I compile the project and can I omit that ?
    2. For debug version of the project, is it necessary to use -g flag?

    Thanks for the answers in advance

    • Hi Zaki!

      1. It enables exception handling. It's enabled by default for projects written in C++, you can ignore it.
      2. If you want to debug your program, yes. -g adds debugging information (eg. function names) to the executable. Without those, it can be hard to figure out what's happening.

  • Bob

    Do we need to worry about this with Xcode? The warnings seem to be already pretty aggressive~

  • I'm in love with alex's tutor

    Dear Mr.Alex!
    Really Thank you so much, this really helps me a lot to find out minor mistakes of mine . This increases a programmer perfection to make his program free against any bugs.
    Bless you forever  Sir!

  • Qluefqen

    When I try to use this flag:

    I get this error:

    So I tried it with

    and got no errors or warnings but am not clear on what that flag is. Can you help?

    Compiler is

    . Running Debian Buster (Testing) KDE and using Kate and Konsole if that makes any diff.

    PS: Made a minor edit and screwed up the formatting without changing it. Sorry.

  • vbot

    Hi,

    enabling all warnings (/Wall) in VS 2017 15.8.5 will throw hundreds of MSVC-warnings that are turned off by default. See:
    https://docs.microsoft.com/en-us/cpp/preprocessor/compiler-warnings-that-are-off-by-default?view=vs-2017

    here it says: "The C runtime libraries and the C++ standard libraries are intended to emit no warnings only at warning level /W4."

    Greets

  • William

    Shouldn't the flag for "treat warnings as errors" to be -Werror? The -Wfatal-errors flag means to stop the compilation once an error occur. Ref: https://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Warning-Options.html#Warning-Options

  • Ryan

    Hello! I may have found a typo, and wanted to point it out in case it was a mistake. When you said "you can request your compiler be more assertive about providing warnings about for things it finds strange", I think you might have accidentally put 'about' a second time.

    • Alex

      No may about it, definitely a typo. Again, thanks for pointing all of these out.

      • Ryan

        My pleasure! I'm glad I could help. I worded it in a subjective way to make sure I wasn't possibly just misinterpreting something and falsely pointing it out, especially since I'm not entirely familiar with the field yet. That's what I'm here to change, though!

  • Ahmed

    Any tut for the following:
    Add the following flag to your command line: -Wfatal-errors

Leave a Comment

Put all code inside code tags: [code]your code here[/code]