skip to content

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%

answer

  1. save the state, change it, restore it
  2. ignored, warning or error
  3. position in the file matters
  4. the bracketed option name
  5. Clang reads the GCC spelling

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.

solid answer

~40 s

`#pragma GCC diagnostic push` saves the current warning state, `#pragma GCC diagnostic ignored "-Wconversion"` turns that warning off, and `#pragma GCC diagnostic pop` restores the saved state. Wrapping a third-party `#include` this way silences the vendor's code while your own code stays under `-Werror`. The manual says the pragmas override command-line options and that GCC tracks their position, so "pragmas occurring after a line do not affect diagnostics caused by that line". Besides `ignored` there are `warning`, which keeps a diagnostic a warning even under `-Werror`, and `error`. Only warnings are controllable, not all of them, and the option name to use is the one GCC prints in brackets after the warning. Clang treats `#pragma GCC diagnostic` as a synonym for its own spelling.

code

cpp · 3 lines
cpp
// vendor/fastmath.h
#pragma once
inline int fast_round(double x) { return x + 0.5; }   // double -> int, implicit

go deeper

for a junior

Recall push, ignored and pop, and that the option name comes from the bracketed tag at the end of the warning.

for a middle

Explain that pragmas act by position, override the command line, and come in ignored, warning and error forms.

for a senior

Choose between pragmas, system include directories and command-line -Wno-error for vendor code, and keep suppressed regions small and documented.

for a principal

Set a team rule for when suppression is allowed, how it is reviewed, and how it survives a second compiler in the build matrix.

## The problem the pragmas solve A project builds with `-Werror` and a strict warning set. Then it includes a header it does not own, a vendor math library or generated code, that triggers one of those warnings. Editing the vendor code is not an option, and dropping the warning project-wide would lose it for your own code. **Diagnostic pragmas** change warning behaviour for a region of a source file instead. ## The three forms The GCC manual documents `#pragma GCC diagnostic kind option`, where `option` is the command-line flag in double quotes and `kind` is one of: - `ignored`: the diagnostic is not issued - `warning`: treat it as a warning, "even if `-Werror` is in effect" - `error`: treat it as an error and the state stack: - `#pragma GCC diagnostic push` remembers the state of every diagnostic - `#pragma GCC diagnostic pop` restores the state saved by the matching `push`; a `pop` with no matching `push` restores the command-line options ## How GCC applies them 1. **They override the command line** for the region they cover. 2. **Position matters.** GCC records where each pragma is and issues diagnostics according to the state at that point, so a pragma placed after a line does not affect that line. 3. **Only warnings are controllable**, "and not all of them". Hard errors cannot be silenced this way. 4. **The name is the bracketed option.** Each controllable warning ends with the option that controls it, such as `[-Wconversion]`; that string goes into the pragma. The manual points to `-fdiagnostics-show-option`, on by default, as the way to see which diagnostics are controllable and by which option. 5. On the command line, the manual adds that options set by pragmas "take effect as if they appeared at the end of the command line". ## The pattern for a third-party header ```cpp #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wconversion" #include "vendor/fastmath.h" #pragma GCC diagnostic pop ``` Everything the header declares, including inline function bodies, is compiled with `-Wconversion` off. Your code after the `pop` is back to the command-line state, `-Werror` included. ## Variants worth knowing | Need | Tool | |---|---| | Silence one warning in one region | `push` / `ignored` / `pop` | | Keep one warning visible but non-fatal in a region | `#pragma GCC diagnostic warning "-Wname"` | | Make one warning fatal in one file only | `#pragma GCC diagnostic error "-Wname"` | | Emit a pragma from a macro | the `_Pragma("GCC diagnostic ignored \"-Wname\"")` operator | | Treat a whole include directory as third-party | include it as a system directory, a driver option covered with the compilation stages | On the last row: GCC normally suppresses warnings in system headers; the manual's entry for `-Wsystem-headers` says so. That is often a cleaner fit for a whole vendored library than pragmas around every include. ## Portability and discipline - **Clang** documents `#pragma GCC diagnostic` and `#pragma clang diagnostic` as synonyms, and Apple clang 21 honoured the example below under `-Werror`. The Clang manual notes GCC ignores `#pragma clang diagnostic`, so the GCC spelling is the portable one. Warning names still differ: a name only one compiler knows becomes an unknown-warning complaint on the other. - Always **pair** `push` with `pop`; an unbalanced `ignored` leaks into everything after it. - Keep the region **as small as possible** and add a comment saying why. - Prefer fixing **your own** code over silencing it; pragmas are for code you cannot change.

  • What happens if you write ignored without push and pop?
    The warning stays off from that line to the end of the translation unit, including every later header and all your own code after it. That is how a pragma meant for one vendor header quietly removes a warning from a whole file.
  • How do you keep one warning non-fatal in a file that builds with -Werror?
    `#pragma GCC diagnostic warning "-Wdeprecated-declarations"` makes that diagnostic a plain warning even when `-Werror` is in effect. On the command line the equivalent is `-Wno-error=deprecated-declarations`.

  • A pragma at the end of a file silences warnings above it
  • #pragma GCC diagnostic ignored can silence hard compile errors
  • ignored without push and pop affects only the next line
  • Clang ignores #pragma GCC diagnostic entirely
  • The pragma needs the warning's message text, not its option name