gcc -Wall -Wextra -Werror: enable compiler warnings
- tool
- C
- command
- gcc
- flag
- -Wall -Wextra -Werror
- default
- off: without flags GCC prints only a small set of default warnings
gcc -Wall -Wextra turns on the warnings that catch most real bugs in C code: unused variables, missing return values, implicit declarations, format string mismatches, comparisons between signed and unsigned values, and uninitialized variables. Despite its name, -Wall is not "all warnings"; -Wextra adds more. -Werror makes every warning an error, which is useful in CI. Clang accepts the same flags.
Usage
gcc -Wall -Wextra -o app main.c # recommended baseline
gcc -Wall -Wextra -Wpedantic -std=c17 -o app main.c # also warn about non-standard extensions
gcc -Wall -Wextra -Werror -o app main.c # fail the build on any warning
gcc -Wall -Wextra -Werror=format -o app main.c # make only one warning group fatal
gcc -Wall -Wextra -Wno-unused-parameter -o app main.c # turn one warning offWhat each flag adds
| Flag | Examples of what it enables |
|---|---|
| -Wall | -Wunused-variable, -Wformat, -Wimplicit-function-declaration, -Wreturn-type, -Wmaybe-uninitialized, -Wparentheses |
| -Wextra | -Wsign-compare, -Wunused-parameter, -Wmissing-field-initializers, -Wtype-limits |
| -Wpedantic | Warnings required by the ISO C standard for extensions |
| -Wshadow | A local variable hides another variable (not in -Wall or -Wextra) |
| -Wconversion | Implicit conversions that may change a value (not in -Wall or -Wextra) |
| -Werror | Treat all warnings as errors |
| -Werror=NAME / -Wno-NAME | Make one warning an error / disable one warning |
Compiling the example below with gcc -Wall -Wextra reports the unused variable, the signed/unsigned comparison and the format mismatch; plain gcc stays quieter. GCC names the option that produced each warning at the end of the message, for example [-Wunused-variable], so you know which -Wno-... would silence it.
Example
#include <stdio.h>
int main(void) {
int unused;
unsigned n = 3;
for (int i = 0; i < n; i++) /* signed vs unsigned comparison: -Wextra */
printf("%s\n", i); /* %s with an int: -Wformat (in -Wall) */
return 0;
}Errors by default in GCC 14
Some diagnostics used to be warnings that only showed up with -Wall, and now stop the build by default. GCC 14 turned implicit function declarations, implicit int, integer/pointer conversions and incompatible pointer types into errors for C99 and later, as described in its porting notes. A missing #include that older GCC merely warned about now fails with error: implicit declaration of function (see implicit declaration of function). The right fix is the missing header or prototype; -fpermissive or -Wno-error=... only hides the problem for legacy code.
Silencing a warning on purpose
Prefer fixing the code over turning a warning off. When a warning is a false positive, narrow the exception as far as possible:
- Cast an intentionally unused parameter to void:
(void)argc;, or mark it with__attribute__((unused))or C23's[[maybe_unused]]. - Disable one warning for one file with
-Wno-NAMEin that file's compile options. - Disable it for a few lines with
#pragma GCC diagnostic push,#pragma GCC diagnostic ignored "-Wformat"and#pragma GCC diagnostic pop; Clang accepts the same pragmas. - Keep a warning visible but non-fatal under
-Werrorwith-Wno-error=NAME.
In a build system
With CMake, add the flags per target so they do not leak into third-party code: target_compile_options(app PRIVATE -Wall -Wextra). Wrap them in a generator expression or a compiler check if the project also builds with MSVC, which does not understand them. With a plain Makefile, append them to CFLAGS.
FAQ
Does -Wall enable all warnings?
No. -Wall is a curated set that GCC considers easy to avoid; -Wextra adds more, and options such as -Wshadow and -Wconversion stay off until you name them. Clang has -Weverything for literally every warning, which is meant for exploring, not for everyday builds.
Should I always build with -Werror?
Use it in CI with a pinned compiler version. A newer GCC can add warnings to -Wall, and for code shipped as source a blanket -Werror then breaks other people's builds for reasons unrelated to them.
What is the MSVC equivalent of -Wall -Wextra -Werror?
MSVC uses /W4 for a high warning level and /WX to treat warnings as errors. Its /Wall enables everything and is very noisy, like Clang's -Weverything.
Sources
- gcc.gnu.orggcc.gnu.org/onlinedocs/gcc/Warning-Options.html
- clang.llvm.orgclang.llvm.org/docs/DiagnosticsReference.html
- clang.llvm.orgclang.llvm.org/docs/UsersManual.html#options-to-control-error-and-warning-messages
- gcc.gnu.orggcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
- gcc.gnu.orggcc.gnu.org/gcc-14/porting_to.html