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


0.12 -- Configuring your compiler: Choosing a language standard
Index
0.10 -- Configuring your compiler: Compiler extensions

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

  • Vitaliy Sh.

    There was a reference to Makefiles in the lesson 0.5, in "For advanced readers".

    Can you please, add some reference to writing the shell scripts for a GCC/g++, for the educational and day-by-day purposes?

    • nascardriver

      This script is compiler-specific and not very human friendly. I don't see the need for it when there are friendlier and compiler-independent alternatives like CMake.

      • Vitaliy Sh.

        You are obviously right...
        I were confused by:
        ... Add the following flag to your command line: -Werror ...
        I did mistake by separating the "start g++ from command line" and the Makefiles as opposite things.

  • Vitaliy Sh.

    ... > Compiler >Compiler settings tab, ...

    Typo: ">Compiler" - no space.

  • Jim

    Hey Alex,
    Have been away and just coming back to C++. I'm using C::B and there is a note in yellow that says: For GCC/G++ users

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

    I didn't see it shown on this lesson so should we add "-Wall -Weffc++ Wextra" to -Wsign-conversion that is shown there also?

    I'm 81 and not very good at programming but I still enjoy your lessons.  Thanks

  • Lee rV

    For laughs, I turned on /wall in a simple project that uses <iostream>, <string>, and <ctype.h>.

    The compiler came back with over 200 warnings! Combined with "Treat warnings as errors", and I was hilariously out of luck, as I'm way too much of a newbie to start re-writing the standard library to be "correct"!

    "Wall", indeed!

    • nascardriver

      Your compiler shouldn't warn you about anything in system headers, only about issues in your own code. This sounds like a misconfigured compiler/project.

  • Anastasia

    Hi!
    Which flags/options it would be recommended to use for clang (command line, no IDE) with as many warnings as possible (not treated as errors though), all compiler extensions turned off, fastest compilation and maybe some additional diagnostic information (if possible)?

    • clang uses the same cli as gcc. If you had those settings for gcc, you can use them for clang as well.

      > as many warnings as possible
      -Weverything
      That's not recommended though, it will flood you with warnings. You can use -Weverything to find the flags that are relevant to you.
      I use

      > all compiler extensions turned off

      > fastest compilation
      If you mean that you want your program to run fast, -O3 is the highest optimization. If you really mean fast compilation, I don't know, I suppose it always tries to be fast. llvm offers a drop-in replacement for ld, you can use that to link faster. You won't notice a difference with small projects.

      > some additional diagnostic information
      clang-tidy might be what you're looking for. It works well with CMake (CMake allows you to configure the compiler without knowing which compiler you're using. You don't need to worry about command line options or manually listing your files every time you want to compile).

      • Anastasia

        > If you had those settings for gcc, you can use them for clang as well.
        Oh, I didn't know that. I browsed quickly through the man pages, but there are only a few flags and options mentioned in comparison to gcc.

        > If you mean that you want your program to run fast
        No, I meant without options that would negatively impact the compilation speed. Optimization does impact it and makes the code harder to debug (the man says).

        > CMake allows you to configure the compiler without knowing which compiler you're using.
        I think it is important now when I'm only learning to do everything manually and understand how things work. I wouldn't learn c++ if I was looking for easy ways :D
        It's noted though for the future.

        > llvm offers a drop-in replacement for ld, you can use that to link faster.
        > clang-tidy might be what you're looking for.
        I'll look into these for sure, thank you!

        edit: @Alex, can you please add to this lesson that clang users should use the same flags as gcc? Thanks!

      • Vitaliy Sh.

        Me isn't want to flood, but:
        In a "Qt Creator" there is preset:
        -Weverything and 12th -Wno-XXXes.

        Is that was a wrong choice? The IDE looks fancy.

        The Clang's "User Manual" says:

        ...
        Using -Weverything means that updating your compiler is more difficult because you’re exposed to experimental diagnostics which might be of lower quality than the default ones.
        ...

        • nascardriver

          -Weverything is more than you should use. The -Wno* might get rid of the unnecessary warnings. Post you code when you solve quizzes and I'll tell you if you're doing something that a warning told you to do when it's actually not good.

  • Marie Bethell

    Hello!
    To save my error and warning settings, I created a user template in Code::Blocks. But I noticed that now in my Code::Blocks folder, instead of each new project having it's own folder, it just dumps all the source files and headers together (no separation between projects). Before each project had it's own folder with all dependent files in it as well. Any idea what's going on here? Thanks!

  • Rahab

    When I choose all configuration I do't find the C/C++ option, therefore I couldn't disable the compiler extensions nor could I do any of the instructions on this lesson. why Can't I find It?

  • In the first paragraph:(assuming you’ve turned off compiler extensions, as per lesson 0.10 -- Configuring your compiler: Compiler extensions).
    Is word 'per' mistyped? It should be 'pre lesson'.

  • Charlton

    If we have -std=c++17 in other compiler options in code::blocks. Should -Wsign-conversion -Werror go on the same line or on a new line.

    -std=c++17 -Wsign-conversion -Werror
    or
    -std=c++17
    -Wsign-conversion -Werror

    Does it even matter?

  • Divyansh

    Do we have to change these settings (mentioned above) every time we make a new project or these setting will be applicable on all new projects ?

  • Flynn

    Hello again, was just wondering how I could go about adding the additional warnings you talked about in the above lesson.
    Thanks in advance, Flynn.

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