gcc Command Cheat Sheet
gcc (GNU Compiler Collection) is the standard compiler for C and C++ on Linux. It transforms source code into executable binaries through preprocessing, compilation, assembly, and linking.
Synopsis
gcc [options] [source_files] [-o output_file]
Basic Compilation
Compile a Simple Program
gcc main.c
a.out (default executable name).
Specify Output Name (-o)
gcc main.c -o myapp
./myapp.
Compile Multiple Files
gcc main.c utils.c -o myapp
Compilation Stages
You can stop the process at different stages.
| Flag | Stage | Output | Description |
|---|---|---|---|
-E |
Preprocessing | stdout (or .i) |
Expands macros, includes headers |
-S |
Compilation | .s (Assembly) |
Generates assembly code |
-c |
Assembly | .o (Object) |
Generates machine code (no linking) |
| (none) | Linking | Binary | Links objects into executable |
Create Object File
Useful for Makefiles to compile only changed files.
gcc -c main.c -o main.o
Debugging and Warnings
Enable All Warnings (-Wall)
Always use this during development!
gcc -Wall main.c -o myapp
Enable Extra Warnings (-Wextra)
Even stricter checks.
gcc -Wall -Wextra main.c -o myapp
Treat Warnings as Errors (-Werror)
Forces you to fix warnings.
gcc -Werror main.c -o myapp
Include Debug Symbols (-g)
Required for using gdb or valgrind.
gcc -g main.c -o myapp
Optimization
Levels of optimization affect compilation time and execution speed.
| Flag | Description | Trade-off |
|---|---|---|
-O0 |
No optimization (default) | Fast compile, slow run, debuggable |
-O1 |
Basic optimization | Balanced |
-O2 |
Recommended for deploy | Good speed, safe |
-O3 |
Aggressive optimization | Best speed, larger binary, potential bugs |
-Os |
Optimize for size | Smallest binary |
-Ofast |
Disregard standards | Fastest, but may break math accuracy |
gcc -O2 main.c -o myapp
Linking Libraries
Link a Standard Library (-l)
To use functions like pow() or sin() from <math.h>, you often need to link libm.
gcc main.c -o myapp -lm
lib prefix and .so/.a extension are omitted. libm.so becomes -lm.
Specify Library Path (-L)
If the library is in a custom directory (e.g., /opt/lib).
gcc main.c -o myapp -L/opt/lib -lmylib
Specify Header Path (-I)
If headers are in a custom directory.
gcc main.c -o myapp -I/opt/include
Preprocessor Macros
Define a Macro (-D)
Equivalent to #define DEBUG 1 in the code.
gcc -DDEBUG main.c -o myapp
Undefine a Macro (-U)
gcc -UDEBUG main.c
Advanced Options
Static Linking (-static)
Embeds all libraries into the binary. Result is larger but portable (no dependency on system .so files).
gcc -static main.c -o myapp_static
Shared Library Creation (-shared)
Create a .so file. Needs Position Independent Code (-fPIC).
gcc -shared -fPIC -o libutils.so utils.c
32-bit Compilation (-m32)
Compile for 32-bit architecture on a 64-bit system (requires gcc-multilib).
gcc -m32 main.c -o myapp32
Practical Examples
Check Header Search Paths
Where is gcc looking for <stdio.h>?
gcc -xc -E -v /dev/null
Generate Dependency Rules (-MM)
Outputs Makefile-compatible dependency lines.
gcc -MM main.c
# Output: main.o: main.c utils.h config.h
Notes
g++: For C++ code, useg++instead ofgcc. It automatically links the C++ standard library.std: Specify language standard with-std.gcc -std=c99 main.c g++ -std=c++11 main.cpp