Lesson 7 +10 XP

Compiling with GCC and Clang

Compiling with GCC and Clang

Compiling is the moment your text becomes a program. Let's look closely at the common options.

The base command

gcc app.c -o app
  • gcc is the compiler.
  • app.c is the source.
  • -o app names the output executable (without it, Linux names it a.out).

Warning flags: your friends

Warnings catch bugs before they bite. Turn them on:

gcc -Wall -Wextra app.c -o app

Standard selection

Modern C dialects add features. C11 is a common good default:

gcc -std=c11 app.c -o app

Debug builds

-g embeds debug info so you can use tools like breakpoints:

gcc -g app.c -o app

Optimization

Release builds get:

gcc -O2 app.c -o app

-O2 makes the code run faster. It's the everyday "speed up my program" flag.

Running

./app

TL;DR

  • gcc app.c -o app produces the executable.
  • -Wall -Wextra turn on important warnings.
  • -std=c11 peeks the language standard.
  • -g is for debugging; -O2 is for speed.