LinuxCommandLibrary

gcc

Compile C and C++ source code

TLDR

Compile multiple source files into an executable

$ gcc [path/to/source1.c path/to/source2.c ...] [[-o|--output]] [path/to/output_executable]
copy

Activate output of all errors and warnings
$ gcc [path/to/source.c] -Wall [[-o|--output]] [output_executable]
copy

Show common warnings, debug symbols in output, and optimize without affecting debugging
$ gcc [path/to/source.c] -Wall [[-g|--debug]] -Og [[-o|--output]] [path/to/output_executable]
copy

Include libraries from a different path
$ gcc [path/to/source.c] [[-o|--output]] [path/to/output_executable] -I[path/to/header] -L[path/to/library] -l[library_name]
copy

Compile source code into Assembler instructions
$ gcc [[-S|--assemble]] [path/to/source.c]
copy

Compile source code into an object file without linking
$ gcc [[-c|--compile]] [path/to/source.c]
copy

Optimize the compiled program for performance
$ gcc [path/to/source.c] -O[1|2|3|fast] [[-o|--output]] [path/to/output_executable]
copy

Display version
$ gcc --version
copy

SYNOPSIS

gcc [options] source-files... [-o output-file] [-llibrary]...

PARAMETERS

-o file
    Specify output file name (executable or object)

-c
    Compile to object file only, no linking

-S
    Stop after compilation, output assembly code

-E
    Preprocess only, output to stdout

-g
    Generate source-level debug info for GDB

-O0
    No optimization (default)

-O1|-O2|-O3
    Increasing levels of optimization for speed/size

-Os
    Optimize for size

-Wall
    Enable common warnings

-Wextra
    Enable extra warnings

-pedantic
    Warn on non-standard extensions

-std=standard
    Specify language standard (e.g., c99, c11, c++17)

-Idir
    Add directory to header include path

-Ldir
    Add directory to library search path

-llib
    Link with library lib

-Dmacro[=def]
    Define macro for preprocessor

-Umacro
    Undefine macro

-fPIC
    Generate position-independent code for shared libs

-shared
    Create shared object file

-pthread
    Enable POSIX threads support

-v
    Verbose mode, show commands executed

--version
    Print compiler version

-static
    Create fully static executable

-march=cpu
    Generate code for specific CPU architecture

-fsanitize=kind
    Enable runtime sanitizers (e.g., address, thread)

DESCRIPTION

gcc is the primary front-end command for the GNU Compiler Collection (GCC), a mature open-source compiler system supporting languages like C, C++, Objective-C, Fortran, Ada, Go, and more.

It orchestrates the full compilation pipeline: preprocessing (cpp), compilation to assembly, assembly (as), and linking (ld) to produce executables, shared libraries, or object files. Widely used on Linux/Unix for building software from source code.

Key strengths include standards compliance (C89/C99/C11/C17/C2x, C++98/11/14/17/20/23), extensive optimizations, cross-compilation, and plugin support. Developers invoke it for single files or via build systems like Make/CMake. Output defaults to a.out unless specified. Debug with -g, profile with -pg. Supports sanitizers for memory/thread errors.

CAVEATS

Resource-intensive for large projects; default settings may enable insecure features like stack guards—use -fstack-protector. Linking order matters for libraries. Not all options portable across architectures.

LINKING STAGES

gcc handles multi-stage process; use -Wl,opt to pass linker flags directly to ld.

CROSS-COMPILATION

Prefix with target triplet, e.g., arm-linux-gcc for ARM binaries.

WARNINGS AS ERRORS

Use -Werror to treat warnings as failures in CI/builds.

HISTORY

Developed by the GNU Project starting 1987 under Richard Stallman to provide free compiler alternative to proprietary ones. First release (GCC 1.0) 1987; evolved into GCC with multi-language support by 1990s. Now at version 14+ (2024), maintained by FSF with global contributors. Pivotal in Linux kernel/toolchain development.

SEE ALSO

g++(1), cpp(1), as(1), ld(1), make(1), nm(1), objdump(1)

Copied to clipboard