Skip to content
UnderDunn Courses

C Programs and Compilation

Programming FoundationsLesson 5 of 875 minutesLab included
Terminal window
gcc --version

If missing:

Terminal window
sudo apt update
sudo apt install build-essential

Create hello.c:

#include <stdio.h>
int main(void) {
puts("hello from C");
return 0;
}

Compile and run:

Terminal window
gcc -Wall -Wextra -Wpedantic -o hello hello.c
./hello
file hello.c hello

The source is text; the output is an ELF executable. Warnings request useful diagnostics. Do not hide them merely because an executable appeared.

Terminal window
gcc -E hello.c -o hello.i
gcc -S hello.i -o hello.s
gcc -c hello.s -o hello.o
gcc hello.o -o hello
file hello.i hello.s hello.o hello

Preprocessing handles directives such as #include; compilation translates C to assembly; assembly encodes an object file; linking combines objects and required libraries into an executable.

Delete the semicolon after puts and observe a compiler error. Rename main and observe the linker failure. Restore and rebuild after each experiment.

Knowledge check

Question 1 of 2

What does the linker primarily combine?

You can identify each build artifact and locate whether a failure belongs to source translation or final linking. Next we revisit familiar control flow under C’s stricter types.