Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. c-programming
  3. toolchains
6 min read

Compilation

Every lesson so far has compiled with a one-line gcc command, and for a single file that is all you need. Real programs are made of many files, with flags you do not want to retype every time, so this lesson covers what gcc actually does and the two build tools that automate it: Make and CMake.

gcc Compilation

Turning .c source into a running program is not one step but four, and gcc normally does all of them for you in a single command:

main.c main.o prog preprocess →compile → assemble link(+ libc)

The preprocessor handles the #includes and #defines from the syntax lesson; the compiler turns the result into assembly; the assembler turns that into an object file (.o) of machine code; and the linker stitches your object files together with the C library into a single executable. For many files, you can compile each to a .o on its own and link them at the end:

main.c
1#include <stdio.h>
2#include "util.h"
3
4int main(void)
5
$ gcc -c util.c$ gcc -c main.c$ gcc main.o util.o -o prog$ ./prog2 + 3 = 5

Each -c compiles one source into an object file without linking; the final gcc links the objects into prog. (For a quick build you can still do it all at once: gcc main.c util.c -o prog.) The flags you will use most:

  • -o <name> — name the output file.
  • -g — include debug info, for GDB and valgrind.
  • -Wall -Wextra — turn on warnings; treat them as bugs you have not hit yet.
  • -O2 — optimize; -O0 (the default) keeps the code easy to debug.
  • -std=c11 — select the language standard.
  • -c — compile to a .o only; -I<dir> adds a header search path; -l<name> links a library; -D<macro> defines a macro, like the -D_POSIX_C_SOURCE=200809L from the File I/O lesson.

Make and Makefile

Typing those commands by hand gets old fast, and recompiling everything after a one-line change is wasteful. Make solves both. You write a Makefile of rules — each a target, the files it depends on, and the command to build it — and make runs only the commands whose inputs have actually changed.

Makefile
1CC = gcc
2CFLAGS = -Wall -Wextra -g
3
4prog: main.o util.o
5	$(CC) $(

A rule is target: prerequisites, then a command on the next line — indented with a real tab, not spaces, which is Make's one famous gotcha. Running make builds the first target; running it again does nothing, because nothing changed:

$ makegcc -Wall -Wextra -g -c main.cgcc -Wall -Wextra -g -c util.cgcc -Wall -Wextra -g -o prog main.o util.o$ makemake: 'prog' is up to date.$ ./prog2 + 3 = 5

That second make rebuilt nothing — change only util.c and make recompiles util.o and relinks, but leaves main.o alone. make clean runs the clean rule to delete the build products.

CMake and CMakeLists.txt

Hand-written Makefiles become painful for large or cross-platform projects — different compilers, libraries in different places, Windows versus Linux. CMake is a layer above: you describe the project in a CMakeLists.txt, and CMake generates the actual build files (Makefiles, or Ninja, or a Visual Studio project) for whatever system you are on.

CMakeLists.txt
1cmake_minimum_required(VERSION 3.10)
2project(demo C)
3
4add_executable(prog main.c util.c)

That is the whole thing: name a minimum version, name the project, and declare an executable built from two sources — no compiler commands at all. You build in two steps, keeping the generated files in their own build/ directory so they never clutter your source:

$ cmake -B build-- Configuring done (0.2s)-- Generating done (0.0s)-- Build files have been written to: ./build$ cmake --build build[ 33%] Building C object CMakeFiles/prog.dir/main.c.o[ 66%] Building C object CMakeFiles/prog.dir/util.c.o[100%] Linking C executable prog[100%] Built target prog$ ./build/prog2 + 3 = 5

cmake -B build configures (detects your compiler and writes the build files into build/); cmake --build build runs the actual compilation. The payoff grows with the project: add a file to add_executable, or a library with target_link_libraries, and CMake handles the details on every platform.

From a one-line gcc to a generated cross-platform build, you now have the whole toolchain. Pair it with Debugging and you have everything you need to build, run, and fix real C — and every challenge in this section.

{
6 printf("2 + 3 = %d\n", add(2, 3));
7 return 0;
8}
CFLAGS
)
-o prog main.o util.o
6
7main.o: main.c util.h
8 $(CC) $(CFLAGS) -c main.c
9
10util.o: util.c util.h
11 $(CC) $(CFLAGS) -c util.c
12
13clean:
14 rm -f prog main.o util.o