GCC C/C++ compiler

Version 14.2.0 · source

Start here

gcc hello.c -o hello
Compile and link
gcc -Wall -Wextra -g -O0 x.c
Warnings plus debug info
gcc -c x.c
Compile to x.o only
gcc a.o b.o -o app
Link objects
gcc -O2 x.c
Optimize
g++ x.cpp -o x
Compile C++
gcc -fsanitize=address -g x.c
Catch memory bugs
gcc --version
Which compiler?

Concepts

  1. x.c source
  2. x.i preprocessed
  3. x.s assembly
  4. x.o object
  5. a.out executable
Each stage has a flag that stops after it
gcc <file.c> -o <out>
Build an executable
gcc -c <file.c>
Compile to an object file
gcc -E <file.c>
Preprocess only, print result
gcc -S <file.c>
Compile to assembly (.s)
gcc <a.o> <b.o> -o <out>
Link objects together
gcc <a.c> <b.c> -o <out>
Compile several files at once
gcc -v <file.c>
Show every sub-command run

Language standard

gcc -std=c17 <file.c>
Pick the C standard
gcc -std=gnu11 <file.c>
C11 plus GNU extensions
g++ -std=c++20 <file.cpp>
Pick the C++ standard
gcc -pedantic <file.c>
Warn on non-standard code

Warnings

gcc -Wall <file.c>
Common warnings
gcc -Wextra <file.c>
Extra warnings on top of -Wall
gcc -Werror <file.c>
Treat warnings as errors
gcc -Werror=<name> <file.c>
Make one warning an error
gcc -Wno-<name> <file.c>
Silence one warning
gcc -Wshadow -Wconversion <file.c>
Stricter, noisier checks
gcc -fanalyzer <file.c>
Static analysis pass (C)

Optimization

gcc -O0 <file.c>
No optimization (default)
gcc -O2 <file.c>
Standard release optimization
gcc -O3 <file.c>
More aggressive, bigger code
gcc -Os <file.c>
Optimize for size
gcc -Og <file.c>
Optimize but stay debuggable
gcc -Ofast <file.c>
-O3 plus unsafe math
Enables -ffast-math, which breaks strict IEEE behaviour (NaN, ordering).
gcc -march=native <file.c>
Tune for this CPU
Binaries may crash on older CPUs.
gcc -flto <file.c>
Link-time optimization
Pass the same -O and -flto flags at compile and link time.

Debug and profile

gcc -g <file.c>
Add debug info for gdb
gcc --coverage <file.c>
Instrument for gcov

Sanitizers

gcc -fsanitize=address -g <file.c>
AddressSanitizer: memory errors
gcc -fsanitize=undefined -g <file.c>
UBSan: undefined behavior
gcc -fsanitize=address,undefined -g <file.c>
Combine ASan and UBSan
gcc -fsanitize=thread -g <file.c>
ThreadSanitizer: data races
Cannot be combined with address.
gcc -I<dir> <file.c>
Add a header search dir
gcc -L<dir> <file.c> -l<name>
Add a library search dir
gcc <file.c> -l<name>
Link libname.so or libname.a
gcc <file.c> -lm
Link the math library
gcc -pthread <file.c>
Enable POSIX threads
gcc -static <file.c>
Link statically
gcc -Wl,<opt> <file.c>
Pass an option to the linker
gcc -Wl,-rpath,<dir> <file.c>
Embed runtime library path
gcc $(pkg-config --cflags --libs <pkg>) <file.c>
Flags from pkg-config

Macros

gcc -D<name> <file.c>
Define a macro as 1
gcc -D<name>=<value> <file.c>
Define a macro with a value
gcc -dM -E -x c /dev/null
List predefined macros

Dependencies and libraries

gcc -MMD -MP -c <file.c>
Write .d dependency file with build
gcc -M <file.c>
Print all header dependencies
gcc -MM <file.c>
Same, skipping system headers
gcc -fPIC -c <file.c>
Position-independent object
gcc -shared -o lib<name>.so <a.o>
Build a shared library
ar rcs lib<name>.a <a.o> <b.o>
Build a static library
LD_LIBRARY_PATH=<dir> ./<app>
Find shared libs at run time

Inspect the compiler

gcc --version
Show the GCC version
gcc -dumpmachine
Show target triplet
gcc -fverbose-asm -S <file.c>
Annotate generated assembly

Recipes

Strict debug build with sanitizers
gcc -std=c17 -Wall -Wextra -Werror \    -g -O0 -fsanitize=address,undefined \    src/*.c -o app
Release build with header dependencies
# compilegcc -O2 -MMD -MP -c src/main.c -o build/main.o# linkgcc build/main.o -o app -lm# make: -include build/*.d
Shared library and a program using it
gcc -fPIC -shared -o libfoo.so foo.cgcc main.c -L. -lfoo -Wl,-rpath,'$ORIGIN' -o app./app
Look at the generated assembly
gcc -O2 -S -fverbose-asm x.c -o x.sgcc -g -c x.c && objdump -dS x.o | less