8.2 - Common GCC/Clang Compiler Flags

GCC (GNU Compiler Collection) and Clang (part of the LLVM project) are the two most widely used compilers for C programming. Understanding their commonly used flags is essential for building, debugging, optimizing, and securing C programs effectively.
Compilation and Linking Basics
These flags control how your source code is compiled and linked.
-c
Compile source files into object files without linking.
gcc -c main.c
-o <file>
Specify the output file name.
gcc main.c -o my_program
-Wall
and -Wextra
Enable a broad set of warning messages.
gcc -Wall -Wextra main.c
-Wall
: Enables most commonly useful warnings.-Wextra
: Enables even more, often stricter, warnings.
Debugging and Diagnostics
These flags help make your code easier to debug or analyze for issues.
-g
Include debug symbols for use with debuggers like GDB or LLDB.
gcc -g main.c
-fno-omit-frame-pointer
Helps with accurate stack traces, useful during profiling and debugging.
gcc -g -fno-omit-frame-pointer main.c
-fdiagnostics-color=always
Enable colored diagnostic output for easier readability.
gcc -fdiagnostics-color=always main.c
Optimization Flags
Use these flags to improve performance of the generated code.
-O0
, -O1
, -O2
, -O3
, -Os
Control optimization levels:
-O0
: No optimization (default for debugging).-O1
: Basic optimizations.-O2
: Moderate optimizations (recommended default).-O3
: Aggressive optimizations (may increase binary size).-Os
: Optimize for size.
Example:
gcc -O2 main.c
Security and Safety Flags
These flags help reduce the chances of runtime vulnerabilities.
-fstack-protector
/ -fstack-protector-strong
/ -fstack-protector-all
Protect against stack buffer overflows by adding stack canaries.
gcc -fstack-protector-strong main.c
-D_FORTIFY_SOURCE=2
Provides lightweight protection against buffer overflows in common functions.
gcc -O2 -D_FORTIFY_SOURCE=2 main.c
-fPIE -pie
Enable Position Independent Executables, useful for ASLR (Address Space Layout Randomization).
gcc -fPIE -pie main.c
Warnings and Code Quality
Useful for writing safe and portable C code.
-pedantic
Warn about any non-standard language usage.
gcc -pedantic main.c
-Werror
Treat all warnings as errors.
gcc -Wall -Werror main.c
-Wshadow
Warn about variable shadowing.
gcc -Wshadow main.c
Language Standards and Compatibility
Use these flags to target specific language versions or compilers.
-std=c99
, -std=c11
, -std=gnu11
, etc.
Set the C language standard version.
gcc -std=c11 main.c
-m32
/ -m64
Compile for 32-bit or 64-bit architectures.
gcc -m32 main.c
Sanitizers
Built-in runtime checks for various classes of bugs.
-fsanitize=address
Enable AddressSanitizer to catch memory errors.
gcc -fsanitize=address -g main.c
-fsanitize=undefined
Enable Undefined Behavior Sanitizer (UBSan).
gcc -fsanitize=undefined -g main.c
You can combine multiple sanitizers:
gcc -fsanitize=address,undefined -g main.c
Link-Time and Whole Program Optimization
Advanced optimization for performance and binary size.
-flto
Enable Link-Time Optimization (LTO).
gcc -O2 -flto main.c
This allows optimization across source files and reduces binary size.
Clang-Specific Flags
Clang shares most flags with GCC but includes some unique options.
-Weverything
Enable all Clang warnings (very verbose).
clang -Weverything main.c
-fcolor-diagnostics
Enable colored output for diagnostics (default in many environments).
clang -fcolor-diagnostics main.c
Summary
Compiler flags can drastically improve the quality, safety, and performance of your C programs. Understanding when and how to use them is crucial for serious C development.
Whether you’re debugging (
-g
), optimizing (-O2
,-flto
), increasing security (-fstack-protector-strong
), or enforcing code quality (-Wall -Werror
), these flags give you powerful control over the compilation process.
GCC and Clang offer rich sets of options. Mastering them helps produce faster, safer, and more maintainable software.