Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
g++ -std=c++23 -Wall -Wextra -Wpedantic -O2 -g main.cpp -o app

Try it

C++ What the compiler does with a risky comparison

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.

C++ Flags change macros you can test

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:

  • -Wshadow finds a local variable that hides another.
  • -Wconversion and -Wsign-conversion find lossy implicit conversions. They are noisy in old code but valuable in new code.
  • -Wnon-virtual-dtor and -Woverloaded-virtual catch common class-hierarchy mistakes.
  • -Wold-style-cast, -Wuseless-cast and -Wcast-align find risky casts.
  • -Wnull-dereference and -Wdouble-promotion catch smaller accidents.
  • -Wformat=2 checks printf-style format strings, including your own wrappers marked with the format attribute.

Optimization levels

  • -O0 compiles fast and keeps your code almost literal. This is the default, and the right choice while debugging.
  • -Og optimizes lightly while keeping debugging pleasant.
  • -O1 and -O2 enable most optimizations. -O2 is the usual setting for release builds.
  • -O3 adds more aggressive inlining and vectorization. It can make code bigger, and it is not always faster.
  • -Os and -Oz (Clang) favor small code.
  • -march=native tunes 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. -Werror on a developer machine breaks as soon as a new compiler version invents a warning. A common rule is -Werror in 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 -O0 and break at -O2. Test both settings and use sanitizers (see Sanitizers).
  • NDEBUG removes assert. CMake's Release configuration adds it. Never put code with side effects inside assert, because it disappears.
  • Debug info is free at run time. -g increases 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, -lname should 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_ASSERTIONS turn some undefined behavior into a clean crash, at a small cost.