skip to content

Warnings and Diagnostics

-Wall is not all warnings, -Wextra adds more, and -Werror turns them into build failures. Interviewers ask which warnings a team should enable and how to read an error.

on this pageshow

explore

questions

6

With GCC, does -Wall turn on every warning, and what do -Wextra and -Wpedantic add on top of it?

level: juniorimportance: must knowfreq 72%

answer

  1. a name older than its meaning
  2. questionable but easy to avoid
  3. -Wextra: unused parameters, missing initializers
  4. -Wpedantic follows the chosen -std
  5. -Wconversion and -Wshadow are in neither

basics

~10 s

No. GCC's -Wall is a curated set of easy-to-fix warnings; -Wextra adds more, such as unused parameters; -Wpedantic reports extensions ISO C++ forbids. Useful warnings like -Wconversion and -Wshadow are in none of them.

solid answer

~30 s

`-Wall` is not all warnings. The GCC manual describes it as the warnings about constructions "that some users consider questionable, and that are easy to avoid": in C++ that includes `-Wunused-variable`, `-Wreturn-type`, `-Wreorder`, `-Wsign-compare`, `-Wparentheses`, `-Wmaybe-uninitialized` and `-Wdelete-non-virtual-dtor`. `-Wextra` adds a second set: `-Wunused-parameter` (with `-Wall`), `-Wmissing-field-initializers`, `-Wimplicit-fallthrough=3`, `-Wdeprecated-copy`, `-Wredundant-move` and more. `-Wpedantic` is different in kind: it reports code that uses extensions the selected `-std` forbids. Warnings such as `-Wconversion`, `-Wsign-conversion`, `-Wshadow`, `-Wold-style-cast` and `-Wnull-dereference` are in none of these and must be named one by one.

code

cpp · 26 lines
cpp
#include <cstdio>
#include <vector>

struct Sprite {
    int width;
    explicit Sprite(int width) : width(width) {}   // GCC -Wshadow: parameter shadows member
};

int to_pixels(double metres, int scale) {
    int px = metres * scale;                        // GCC -Wconversion: double to int
    return px;
}

void on_resize(int w, int h) {                      // -Wall -Wextra: unused parameter 'h'
    std::printf("w=%d\n", w);
}

int main() {
    std::vector<int> frame_ms{16, 17, 33};
    int unused = 0;                                 // -Wall: unused variable
    for (int i = 0; i < frame_ms.size(); ++i)       // GCC -Wall in C++: signed/unsigned compare
        std::printf("%d\n", frame_ms[i]);
    on_resize(800, 600);
    Sprite s(32);
    std::printf("%d %d\n", to_pixels(1.5, 10), s.width);
}

go deeper

for a junior

Know that -Wall is a baseline, that -Wextra adds more, and that every warning names its flag in brackets so you can look it up.

for a middle

Explain a few members of each set, why -Wpedantic is about ISO conformance, and how specific flags override umbrella flags.

for a senior

Show you know which valuable warnings live outside both sets, such as conversion and shadowing, and the noise they cost on existing code.

for a principal

Frame the choice as a team baseline: which opt-in warnings are worth their noise, and how Clang's different groups affect a dual-compiler build.

## What -Wall actually means The name is historical. The GCC 16 manual describes `-Wall` as enabling "all the warnings about constructions that some users consider questionable, and that are easy to avoid", and then lists them. It says plainly that "some warning flags are not implied by `-Wall`": some warn about code most people consider fine, others about code that is necessary or hard to change. So `-Wall` is a **curated baseline**, not a complete set. For a C++ build, the GCC 16 list includes, among many others: - `-Wunused-variable`, `-Wunused-function`, `-Wunused-but-set-variable` - `-Wreturn-type` (falling off the end of a non-void function) - `-Wreorder` (member initializers written in a different order than the members are declared) - `-Wsign-compare` (in C++ this comes with `-Wall`; in C it comes with `-Wextra`) - `-Wparentheses`, `-Wmisleading-indentation`, `-Wswitch` - `-Wmaybe-uninitialized` and `-Wuninitialized` - `-Wdelete-non-virtual-dtor`, `-Wpessimizing-move`, `-Wself-move`, `-Wrange-loop-construct` - `-Wnarrowing` and `-Woverloaded-virtual=1` Some entries only work with optimization: the manual marks `-Warray-bounds=1` as "only with `-O2`". ## What -Wextra adds `-Wextra` (formerly spelled `-W`) enables "some extra warning flags that are not enabled by `-Wall`". For C++ the GCC 16 list includes: - `-Wunused-parameter`, which fires only when `-Wall` or `-Wunused` is also on - `-Wmissing-field-initializers` and `-Wimplicit-fallthrough=3` - `-Wdeprecated-copy`, `-Wredundant-move`, `-Wdangling-reference` and `-Wsized-deallocation` - `-Wtype-limits`, `-Wempty-body`, `-Wignored-qualifiers` and `-Wcast-function-type` It also warns about a few cases with no flag of their own, such as a base class not initialized in a derived class's copy constructor. ## -Wpedantic is a different kind of flag `-Wpedantic` (also `-pedantic`) issues "all the warnings demanded by strict ISO C and ISO C++" and diagnoses programs that use forbidden extensions, following the `-std=` you selected. With `-std=gnu++20` or `-std=c++20`, GNU extensions such as variable-length arrays in C++ or zero-length arrays are accepted silently without it; with it they are diagnosed, and `-pedantic-errors` turns those into errors. It is about **conformance**, not code quality. ## What none of them turn on | Flag | What it reports | In `-Wall`? | In `-Wextra`? | |---|---|---|---| | `-Wconversion` | implicit conversions that may change a value | no | no | | `-Wsign-conversion` | implicit signed/unsigned changes (C++) | no | no | | `-Wshadow` | a declaration hiding another variable or member | no | no | | `-Wold-style-cast` | C-style casts in C++ | no | no | | `-Wnull-dereference` | paths that dereference null | no | no | | `-Wnon-virtual-dtor` | polymorphic class without virtual destructor | no | no | | `-Wuseless-cast` | a cast to the expression's own type | no | no | A team that wants these must add them by name. ## How the flags combine 1. `-Wall` and `-Wextra` are **umbrella** flags that turn on other flags, some of which turn on further ones. 2. The manual's rule: "more specific options have priority over less specific ones, independently of their position in the command line". So `-Wno-unused-parameter -Wextra` still leaves unused-parameter warnings off. 3. For options of the same specificity, the last one wins. 4. Every controllable warning prints its controlling option in brackets, which is the name to use with `-Wno-`, `-Werror=` or a pragma. ## Clang is close, not identical Clang accepts `-Wall`, `-Wextra` and `-Wpedantic` too, but its groups are its own. Running Apple clang 21 on the example below, `-Wall` alone did not report the signed/unsigned loop comparison; adding `-Wextra` did. Clang also has `-Weverything`, which GCC does not. A flag set tuned on one compiler is not automatically equivalent on the other.

  • Why does GCC not put every warning into -Wall?
    The manual says some warnings flag code most users do not consider questionable, and others flag constructs that are necessary or hard to avoid with no simple rewrite to silence them. Putting those in the default set would bury real problems under noise, so they stay opt-in and a team enables them deliberately.
  • You pass -Wextra but never see unused-parameter warnings; why?
    In GCC, `-Wunused-parameter` from `-Wextra` only fires together with `-Wunused` or `-Wall`. Passing `-Wall -Wextra` turns it on. A more specific `-Wno-unused-parameter` anywhere on the command line also wins over the umbrella flags, regardless of order.
  • Does a clean -Wall -Wextra build mean the code is conversion-safe?
    No. Neither umbrella enables `-Wconversion` or, in C++, `-Wsign-conversion`, so a `double` silently truncated to `int` or a negative `int` turned into a huge `unsigned` passes both. Those flags have to be added by name, and they are usually noisy on older code.

  • -Wall enables every warning GCC implements
  • -Wextra replaces -Wall, so passing both is redundant
  • -Wpedantic checks code style such as naming and casts
  • A clean -Wall build rules out lossy implicit conversions
  • Flag order decides whether -Wno-unused-parameter beats -Wextra

Assumes GCC 16.2; the members of -Wall and -Wextra listed here are taken from its manual, and the sets grow between releases.

open as a page

With GCC, how do #pragma GCC diagnostic push, ignored and pop silence one third-party header's warning under -Werror?

level: middleimportance: should knowfreq 38%

basics

~20 s

Wrap the #include in #pragma GCC diagnostic push, then ignored "-Wname", then pop: GCC ignores that warning only between them and keeps -Werror elsewhere. A pragma affects only diagnostics for lines after it, and pop restores the saved state.

open as a page

With GCC, how do you read a hundred-line error from calling std::sort on a std::list and find the line to fix?

level: middleimportance: should knowfreq 48%

basics

~20 s

Read the first error, then follow its instantiation notes to the one frame in your own source: that is the call to fix. Here std::sort needs random-access iterators and std::list has bidirectional ones, so call the list's own sort() instead.

open as a page

With GCC, what do -Wconversion and -Wshadow catch that -Wall -Wextra miss, and why does C++ need -Wsign-conversion separately?

level: middleimportance: should knowfreq 50%

basics

~20 s

-Wconversion warns on implicit conversions that may change a value, such as double to int; -Wshadow warns when a declaration hides another variable, parameter or class member. In C++, GCC's -Wconversion skips signed/unsigned changes unless -Wsign-conversion is enabled explicitly.

open as a page

With GCC 16, why can a C++ build pass -Werror at -O0 but fail at -O2 on -Wmaybe-uninitialized, and what does -fanalyzer add?

level: seniorimportance: should knowfreq 34%

basics

~20 s

Some GCC warnings use data-flow analysis that only runs when optimizing: -Wmaybe-uninitialized is impossible at -O0, so an -O2 build sees new reports. -fanalyzer adds costly path-based checking, but the GCC 16 manual calls it suitable only for C code.

open as a page

You lead a C++ codebase with 4,000 GCC warnings: how do you choose the team's warning set and roll out -Werror without stalling delivery?

level: principalimportance: should knowfreq 40%

basics

~20 s

Fix the warning set first, freeze today's count, then ratchet: -Werror=<flag> for categories already clean, -Wno-error=<flag> for noisy ones, and new code held clean. Enforce -Werror in your CI, not in flags that users or newer compilers inherit.

open as a page