C Academy · Lesson

Compiling Multiple Files

Build a multi-file program.

Lesson 4 of 413 steps

Compiling Multiple Files is a free C Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.

From Source to Program

Turning multi-file C source into an executable happens in two stages: compiling and linking.

Each .c file is compiled separately into an object file. Then the linker combines all object files, resolving references between them, into one runnable program.

The One-Command Build

The simplest approach hands every source file to gcc at once. The compiler builds each, then links them automatically.

The -o flag names the output program. List all .c files; headers are pulled in via #include and are not listed here.

gcc main.c math_utils.c -o app

Separate Compilation

For larger projects, compile each file to an object file with -c. This produces a .o file and does not link.

Compiling separately means changing one source only requires recompiling that one file, which speeds up large builds.

gcc -c main.c        /* produces main.o */
gcc -c math_utils.c  /* produces math_utils.o */

The Linking Step

Once you have object files, link them into an executable. Here gcc acts as the linker, joining the objects and resolving cross-file symbols.

No -c this time, because we want a final program, not another object file.

gcc main.o math_utils.o -o app

What the Linker Does

The linker matches each use of a symbol to its definition.

When main.o calls add, the linker finds the add defined in math_utils.o and connects them. It also pulls in standard library code your program needs.

Undefined Reference Errors

If you forget an object file when linking, the linker cannot find a definition and reports an undefined reference.

This is a link-time error, not a compile error: each file compiled fine, but a needed definition was missing from the final command.

gcc main.o -o app
/* undefined reference to `add' */
/* fix: include math_utils.o */

Helpful Warning Flags

Always compile with warnings enabled. -Wall and -Wextra surface likely bugs early.

Adding -std=c11 selects a language standard so behavior is predictable across compilers. These flags work whether you compile in one step or separately.

gcc -Wall -Wextra -std=c11 -c main.c
gcc -Wall -Wextra -std=c11 -c math_utils.c
gcc main.o math_utils.o -o app

Include Paths

When headers live in another directory, tell the compiler where to look with -I.

For example -Iinclude adds an include/ folder to the search path, so #include "math_utils.h" resolves even from a different folder.

gcc -Iinclude -c src/main.c -o build/main.o

Why Recompilation Matters

Separate compilation shines when only one file changes. You recompile just that file's object, then relink.

If you edit a header, though, every source that includes it must be recompiled, because the header's text is part of each of them.

/* edited math_utils.c only */
gcc -c math_utils.c
gcc main.o math_utils.o -o app

Automating With make

Typing commands by hand gets tedious. A Makefile records the rules and rebuilds only what changed.

This minimal example links two objects into app; make tracks file timestamps to decide what to recompile.

app: main.o math_utils.o
	gcc main.o math_utils.o -o app

main.o: main.c math_utils.h
	gcc -c main.c

A Full Build Recap

Here is the whole flow for a small project, from objects to a finished program with warnings on.

Compile each source, then link the objects. Run the result to confirm it works end to end.

gcc -Wall -c main.c
gcc -Wall -c math_utils.c
gcc main.o math_utils.o -o app
./app

Quick Check

Check your understanding of the multi-file build process.

Recap

You learned to compile each .c file into an object with -c, then link the objects into a program. Use -Wall and -I for warnings and include paths.

Undefined references mean a missing object at link time. Tools like make automate rebuilding only what changed.

Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Compiling Multiple Files” lesson free?

Yes — the full text of “Compiling Multiple Files” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “Compiling Multiple Files”?

Build a multi-file program. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Compiling Multiple Files” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Header and Source Files
  2. Include Guards
  3. extern and Linkage
  4. Compiling Multiple Files
← Back to C Academy