gcc(1)
GNU C compiler that translates C source code into executable programs or object files.
Synopsis
gcc [OPTION]... FILE...Description
GCC (GNU Compiler Collection) is the primary C compiler on Linux systems. It reads C source files, preprocesses them, compiles to assembly, assembles to object code, and links to produce executables or libraries. GCC supports optimization levels, debugging symbols, and extensive warnings.
By default, gcc produces an executable named a.out. Use -o to specify output filename. Source files ending in .c are treated as C code; .o files are linked as object code.
Common options
| Flag | What it does |
|---|---|
-o FILE | Write output to FILE instead of a.out |
-c | Compile only; produce object file (.o) without linking |
-O0, -O1, -O2, -O3 | Optimization levels (0=none, 3=aggressive); -O2 is typical production default |
-g | Include debugging symbols for use with gdb |
-Wall | Enable most common compiler warnings |
-Wextra | Enable extra warnings beyond -Wall |
-I DIR | Add DIR to header file search path |
-L DIR | Add DIR to library search path |
-l LIB | Link with library libLIB.a or libLIB.so |
-D NAME=VALUE | Define preprocessor macro NAME with optional VALUE |
-std=STD | Use language standard (c89, c99, c11, c17, etc.) |
-E | Preprocess only; output to stdout |
Examples
Compile hello.c and produce executable named hello
gcc hello.c -o helloCompile main.c and utils.c to object files main.o and utils.o (no linking)
gcc -c main.c utils.cLink object files main.o and utils.o to create executable program
gcc main.o utils.o -o programCompile with warnings enabled, debug symbols, and optimization level 2
gcc -Wall -g -O2 program.c -o programCompile source.c and link with the math library (libm) to create calc
gcc source.c -lm -o calcCompile with custom include and library directories, link custom library
gcc -I./include -L./lib source.c -lmylib -o appPreprocess config.c and display first 20 lines (useful for checking macro expansion)
gcc -E config.c | head -20Compile using C11 standard with strict warnings
gcc -std=c11 -Wall modern.c -o modern