$linuxjunkies
>

gcc(1)

GNU C compiler that translates C source code into executable programs or object files.

UbuntuDebianFedoraArch

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

FlagWhat it does
-o FILEWrite output to FILE instead of a.out
-cCompile only; produce object file (.o) without linking
-O0, -O1, -O2, -O3Optimization levels (0=none, 3=aggressive); -O2 is typical production default
-gInclude debugging symbols for use with gdb
-WallEnable most common compiler warnings
-WextraEnable extra warnings beyond -Wall
-I DIRAdd DIR to header file search path
-L DIRAdd DIR to library search path
-l LIBLink with library libLIB.a or libLIB.so
-D NAME=VALUEDefine preprocessor macro NAME with optional VALUE
-std=STDUse language standard (c89, c99, c11, c17, etc.)
-EPreprocess only; output to stdout

Examples

Compile hello.c and produce executable named hello

gcc hello.c -o hello

Compile main.c and utils.c to object files main.o and utils.o (no linking)

gcc -c main.c utils.c

Link object files main.o and utils.o to create executable program

gcc main.o utils.o -o program

Compile with warnings enabled, debug symbols, and optimization level 2

gcc -Wall -g -O2 program.c -o program

Compile source.c and link with the math library (libm) to create calc

gcc source.c -lm -o calc

Compile with custom include and library directories, link custom library

gcc -I./include -L./lib source.c -lmylib -o app

Preprocess config.c and display first 20 lines (useful for checking macro expansion)

gcc -E config.c | head -20

Compile using C11 standard with strict warnings

gcc -std=c11 -Wall modern.c -o modern

Related commands