C Programs and Compilation
Install the toolchain
Section titled “Install the toolchain”gcc --versionIf missing:
sudo apt updatesudo apt install build-essentialCreate hello.c:
#include <stdio.h>
int main(void) { puts("hello from C"); return 0;}Compile and run:
gcc -Wall -Wextra -Wpedantic -o hello hello.c./hellofile hello.c helloThe source is text; the output is an ELF executable. Warnings request useful diagnostics. Do not hide them merely because an executable appeared.
The build has stages
Section titled “The build has stages”gcc -E hello.c -o hello.igcc -S hello.i -o hello.sgcc -c hello.s -o hello.ogcc hello.o -o hellofile hello.i hello.s hello.o helloPreprocessing 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
Knowledge check complete
You answered all 2 questions correctly.
What this gives us as researchers
Section titled “What this gives us as researchers”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.