C++ Reference ยท Preprocessor, Tools and Concepts
C++ compiler flags
A practical guide to GCC and Clang flags for C++: language standard, warning sets, optimization levels, debug info, NDEBUG, sanitizers and hardening options.
Compiler flags decide which language you are compiling, how much the compiler complains, how hard it optimizes, and how much information it leaves behind for debuggers. A handful of them belong in every project; the rest solve specific problems.
The flags on this page are written for GCC and Clang, which share almost all of them. MSVC uses a different syntax (/W4, /O2, /std:c++20), and the equivalents are listed in the table below.
g++ -std=c++23 -Wall -Wextra -Wpedantic -O2 -g main.cpp -o app
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
If the first example prints a warning such as comparison of integer expressions of different signedness, the compiler is doing exactly what -Wall -Wextra is meant to do. The program is still valid, and the warning shows where a negative int would be converted to a huge unsigned value.
The flags you need most
| Purpose | GCC / Clang | MSVC |
|---|---|---|
| Language standard | -std=c++23 (-std=c++20, -std=c++17) |
/std:c++latest, /std:c++20 |
| Common warnings | -Wall -Wextra |
/W4 |
| Pedantic ISO conformance | -Wpedantic |
/permissive- |
| Treat warnings as errors | -Werror |
/WX |
| Optimization levels | -O0 -O1 -O2 -O3 -Os -Og |
/Od /O1 /O2 /Ox |
| Debug information | -g |
/Zi |
| Define a macro | -DNAME=value |
/DNAME=value |
| Disable asserts | -DNDEBUG |
/DNDEBUG |
| Include directory | -Idir |
/Idir |
| Library path and library | -Ldir -lname |
/LIBPATH:dir name.lib |
| Compile only | -c |
/c |
| Link-time optimization | -flto |
/GL with /LTCG |
| Sanitizers | -fsanitize=address,undefined |
/fsanitize=address |
More warnings worth enabling
-Wall is a misleading name: it enables a curated set, not everything. These are usually worth adding to new code:
-Wshadowfinds a local variable that hides another.-Wconversionand-Wsign-conversionfind lossy implicit conversions. They are noisy in old code but valuable in new code.-Wnon-virtual-dtorand-Woverloaded-virtualcatch common class-hierarchy mistakes.-Wold-style-cast,-Wuseless-castand-Wcast-alignfind risky casts.-Wnull-dereferenceand-Wdouble-promotioncatch smaller accidents.-Wformat=2checks printf-style format strings, including your own wrappers marked with theformatattribute.
Optimization levels
-O0compiles fast and keeps your code almost literal. This is the default, and the right choice while debugging.-Ogoptimizes lightly while keeping debugging pleasant.-O1and-O2enable most optimizations.-O2is the usual setting for release builds.-O3adds more aggressive inlining and vectorization. It can make code bigger, and it is not always faster.-Osand-Oz(Clang) favor small code.-march=nativetunes for the machine running the compiler. The binary may crash on older CPUs, so do not use it for software you ship.
Usage notes
- Treat warnings as errors only in CI.
-Werroron a developer machine breaks as soon as a new compiler version invents a warning. A common rule is-Werrorin your own CI, never in the flags you give to users. - Undefined behavior depends on the optimizer. Code with signed overflow or an out-of-bounds read may "work" at
-O0and break at-O2. Test both settings and use sanitizers (see Sanitizers). NDEBUGremovesassert. CMake'sReleaseconfiguration adds it. Never put code with side effects insideassert, because it disappears.- Debug info is free at run time.
-gincreases file size only. Keep it on in release builds and strip it later, so crash reports can be symbolized. - Order matters for libraries. On Linux,
-lnameshould come after the object files that use it. - Flags are per compilation, not per file. The same macro or standard must be used for every file of a program, or you risk One Definition Rule violations (see Compiling and linking).
- Hardening flags such as
-fstack-protector-strong,-D_FORTIFY_SOURCE=2(needs-O1+) and-D_GLIBCXX_ASSERTIONSturn some undefined behavior into a clean crash, at a small cost.