skip to content

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%

answer

  1. pick the set before the switch
  2. ratchet per category
  3. -Werror= implies the warning
  4. -Wno-error= implies nothing
  5. CI gate, not users' builds

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.

solid answer

~40 s

I would start with `-Wall -Wextra` plus a short list of opt-ins the team agrees are worth their noise, for example `-Wconversion`, `-Wshadow=local` and `-Wnon-virtual-dtor`. Then measure warnings per category. Categories at zero become `-Werror=<flag>` at once; GCC's `-Werror=foo` also enables `-Wfoo`. Noisy categories stay visible but non-fatal with `-Wno-error=<flag>`, which per the manual "does not imply anything", and get burned down module by module. Third-party code is handled with system include directories or scoped pragmas, not by weakening the set. `-Werror` lives in the CI job at the optimization level we ship, not in build flags a downstream user inherits, because a compiler upgrade adds warnings to unchanged code. The cost is a period of mixed rules; the gain is that the count can only go down.

code

bash · 6 lines
bash
# Gate now: these categories are already at zero
WARN_GATE="-Werror=return-type -Werror=delete-non-virtual-dtor -Werror=reorder"
# Burn down: visible on every build, not yet fatal
WARN_BURN="-Wsign-conversion -Wshadow -Wno-error=sign-conversion -Wno-error=shadow"

g++ -std=c++23 -O2 -Wall -Wextra -Werror $WARN_GATE $WARN_BURN -c src/order_book.cpp

go deeper

for a junior

Know that -Werror turns warnings into errors and that -Wno-error=name exempts one warning from that.

for a middle

Explain that -Werror=name also enables the warning, that -Wno-error=name enables nothing, and that specific flags win over umbrella ones.

for a senior

Plan a per-category ratchet, keep vendor code out of scope, and run the gate at the shipped optimization level with a pinned compiler.

for a principal

Own the trade-off between enforcement speed and churn, where -Werror lives, and how compiler upgrades and a second compiler are handled.

## The decision A legacy codebase with 4,000 warnings has two problems: nobody reads the warnings, so new real bugs hide among old noise, and turning on `-Werror` in one step would stop every team at once. A lead has to decide **which warnings matter**, **how fast to enforce them**, and **where enforcement lives**. There is no single right answer; the choices trade churn against safety. ## Step 1: choose the set before the switch - **Baseline:** `-Wall -Wextra`. Both are curated to flag code that is easy to fix. - **Opt-ins worth arguing about:** `-Wconversion`, `-Wsign-conversion`, `-Wshadow` or `-Wshadow=local`, `-Wnon-virtual-dtor`, `-Wold-style-cast`, `-Wnull-dereference`. Each finds real bugs and each is noisy on old code. - **Probably not:** flags whose findings the team will not fix. A warning nobody acts on trains people to ignore the log. | Tier | Example flags | Treatment | |---|---|---| | Gate now | `-Werror=return-type`, `-Werror=delete-non-virtual-dtor` | error immediately | | Burn down | `-Wsign-conversion`, `-Wshadow=local` | visible, `-Wno-error=` until zero | | Advisory | `-Wuseless-cast`, `-Wold-style-cast` | report, track the trend | ## Step 2: the GCC mechanics of a ratchet The manual's rules make an incremental rollout possible: 1. **`-Werror=foo` turns one warning into an error** and "automatically implies `-Wfoo`", so one flag both enables and enforces a category. 2. **`-Wno-error=foo` keeps one warning non-fatal** under a global `-Werror`, and "does not imply anything"; it never enables the warning. 3. **Specific beats general** "independently of their position in the command line", so a category-level flag is not undone by a later umbrella flag. 4. **Unknown `-Wno-` names are tolerated:** GCC reports nothing for an unrecognized `-Wno-foo` unless other diagnostics are produced, which lets one flag list serve old and new compilers. 5. **Pragmas** act as if they appeared at the end of the command line, so a scoped `#pragma GCC diagnostic` exception is precise. A common sequence: global `-Werror` plus `-Wno-error=` for every noisy category; each time a category hits zero, delete its `-Wno-error=` line. The count can only fall. ## Step 3: where -Werror lives - **In CI, not in shipped build files.** A library whose public build flags include `-Werror` breaks for users on a newer GCC that warns about code nobody changed. The CI job uses a pinned compiler; upgrading the compiler becomes a deliberate task with its own warning cleanup. - **At the optimization level you ship.** Some warnings, such as `-Wmaybe-uninitialized`, only exist in optimizing compilation; an `-O0` gate misses them. - **Not in sanitizer jobs.** The GCC manual recommends against combining `-Werror` with sanitizers because they raise false positives. - **Third-party code is out of scope.** Include vendor directories as system headers, where GCC suppresses warnings by default, or wrap the includes in `push`/`ignored`/`pop` pragmas. ## Step 4: the human side - Publish the **policy and the count** so progress is visible. - Make suppression **reviewable**: a pragma needs a comment and a reviewer, and blanket `-w` is banned. - Assign **burn-down by module owner**, not to one hero. - Give teams a **date**, not a surprise, for each category moving from `-Wno-error=` to error. ## The trade-offs to own - **Speed versus churn:** enforcing `-Wsign-conversion` touches thousands of lines; each touch risks a behaviour change in code with weak tests. - **Strictness versus portability:** a set tuned for GCC reports differently under Clang; a dual-compiler project gates on each compiler's own list. - **Pinned versus current compiler:** pinning keeps the gate stable; staying current finds more bugs but means regular cleanup.

  • Why not just add -Werror and fix everything in one sprint?
    With 4,000 warnings, a one-shot fix touches code across every team at once, often in modules with weak tests, so the cleanup itself introduces bugs and blocks delivery. A per-category ratchet lets each category reach zero with focused review and keeps new code clean from day one.
  • How do you stop a compiler upgrade from breaking the gate?
    Pin the GCC version the CI gate uses and treat an upgrade as its own change: build with the new compiler, triage the new warnings, then switch. Keep `-Werror` out of build flags downstream users inherit, so their newer compiler cannot break their builds of your unchanged code.
  • Does -Wno-error=shadow turn on shadowing warnings?
    No. In GCC, `-Wno-error=foo` only changes how a warning is treated if it is enabled; the manual says it implies nothing. You still need `-Wshadow` or `-Wshadow=local` to get the warnings at all.

  • Turn on -Werror globally today and let teams sort it out
  • -Wno-error=foo also enables the foo warning
  • Put -Werror in the library's exported build flags
  • Gate warnings only in the fast -O0 debug build
  • Silence vendor warnings by dropping the flags project-wide