Site Tools


wiki:gcc-flags

Table of Contents

GCC Flags Reference

Optimization flags:

-O0, -O1, -O2, -O3  optimization level (0=none, 3=aggressive)
-Os                 optimize for code size
-Ofast              aggressive, may violate language semantics
-fprofile-generate   instrument for profiling
-fprofile-use        optimize using profiling data
-fopt-info-vec       report vectorization details
-flto                link-time optimization
-fno-inline          disable function inlining

Target and SIMD:

-march=native       use CPU's native instruction set
-march=armv7-a      target specific architecture (ARM, x86, RISC-V)
-mtune=cortex-a9    tune for specific CPU
-mfpu=neon          enable NEON (ARM SIMD)
-mavx2              enable AVX2 (x86 SIMD)
-fno-tree-vectorize disable auto-vectorization

Debug and profiling:

-g                  include debug symbols
-gdwarf-4           use DWARF version 4 debug info
-pg                 enable gprof profiling
-fsanitize=address  enable AddressSanitizer (memory errors)
-fsanitize=thread   enable ThreadSanitizer (data races)

Warnings:

-Wall               common warnings
-Wextra             additional warnings
-Wpedantic          non-standard code
-Werror             treat warnings as errors
-Wuninitialized     warn about uninitialized variables
-Wshadow            warn about variable shadowing
-Wstrict-overflow   warn about signed integer overflow
-Wformat            warn about printf format mismatches

Output:

-o filename         output filename (default: a.out)
-c                  compile only, produce .o object file
-S                  compile to assembly (.s file)
-E                  preprocess only
-fverbose-asm       annotate assembly with source variables

Linking:

-I/path             add include search path
-L/path             add library search path
-lname              link against libname.a or libname.so
-static             link statically (not dynamic)
-shared             produce shared library (.so)
-Wl,--as-needed     linker flag: only link needed libraries

Useful combinations:

# Debug build
gcc -g -O0 -Wall -o app app.c
 
# Production build with warnings
gcc -O3 -Wall -Wextra -o app app.c
 
# HPC build (optimize, vectorize, tune)
gcc -O3 -march=native -fopt-info-vec-missed -o app app.c
 
# Cross-compile for ARM
arm-linux-gnueabihf-gcc -O3 -march=armv7-a -o app app.c
 
# Profile-guided optimization
gcc -O3 -fprofile-generate -o app app.c
./app < input
gcc -O3 -fprofile-use -o app app.c
 
# LTO build (slower compile, faster binary)
gcc -O3 -flto -o app a.c b.c c.c

Checking which flags are available:

gcc -march=help     # list available architectures
gcc --version       # show GCC version and target
man gcc             # full manual page
wiki/gcc-flags.md · Last modified: by 127.0.0.1