After this lesson, you will be able to: Understand the preprocessor → compiler → linker pipeline; write Makefiles + headers with include guards.
Compilation isn't a single step. Understanding the phases unlocks debugging build errors.
1. Preprocessing (cpp): #include, #define, #ifdef substitution. Output: 'translation unit'. 2. Compilation (cc1): C → assembly. Output: .s file. 3. Assembly (as): assembly → machine code. Output: .o object file. 4. Linking (ld): combine .o files + libraries → executable. `gcc main.c -o main` runs all four. Use -E to stop at preprocessing, -S for assembly, -c for object only.
How C projects split across files.
// math_utils.h#ifndef MATH_UTILS_H#define MATH_UTILS_Hint add(int a, int b);int sub(int a, int b);#endif// math_utils.c#include "math_utils.h"int add(int a, int b) { return a + b; }int sub(int a, int b) { return a - b; }// main.c#include <stdio.h>#include "math_utils.h"int main(void) {printf("%d\n", add(2, 3));return 0;}// Compile// gcc -c math_utils.c -o math_utils.o// gcc -c main.c -o main.o// gcc main.o math_utils.o -o app
Reproducible builds without typing commands.
# MakefileCC = gccCFLAGS = -Wall -Wextra -std=c17 -gLDFLAGS = -lmSRCS = main.c math_utils.cOBJS = $(SRCS:.c=.o)TARGET = appall: $(TARGET)$(TARGET): $(OBJS)$(CC) $(OBJS) -o $(TARGET) $(LDFLAGS)%.o: %.c$(CC) $(CFLAGS) -c $< -o $@clean:rm -f $(OBJS) $(TARGET).PHONY: all clean# Use:# make, build# make clean, remove build artifacts# Modern alternative: CMake (covered in C++ sub-track)
'undefined reference to X': you used X but didn't link the .o that defines it (or the library). 'multiple definition of X': X is defined in two translation units. Mark static (if local) or put in a single .c. Headers should declare; .c files should define.
Missing include guards, multiple-include errors. Defining functions in headers (vs declaring). Multiple-definition linker errors. Forgetting -lm for math.h. Hand-typing gcc commands. Makefiles or CMake save real time on bigger projects.
Pick the cleanest answer.
Sign in and purchase access to unlock this lesson.