gcc

Table of Contents

1. Basics

  • Specify directory for Include files/Header files
gcc -Iproj/src main.c -o main # eg. proj/src/myheader.h
  • Optimization. Defaults to -O0 if not specified.

    • Sets to -O1 if flag is -O1 or simply -O.
    • Other optimizations include -O2, -O3, -Os, -Ofast
    gcc -O main.c -o main
    

2. Debug

  • Macro in the fly for preprocessor
gcc -D DEBUG main.c -o main
  • Generate debug info for GDB, Levels: -g0 (negates debug info), -g1, -g, -g3
gcc -g main.c -o main # .debug and .line sections are added
  • The –help flags takes in several classes (see man gcc)

    gcc --help=warnings # Shows warning descriptions
    gcc -Wall main.c -o main # all warnings (Actually not all! see --help=warnings)
    gcc -Wall -Wextra main.c -o main # prints additional warnings
    

3. Linker

  • Passing options to the linker, Wl looks like a Warning flag but is not!
gcc -Wl,-Map main.c # passes -Map to the linker(ld).

Created: 2024-07-16 Tue 16:44

Validate