Post

Complete Guide to Makefiles: From Basics to Advanced

Complete Guide to Makefiles: From Basics to Advanced

Complete Guide to Makefiles

Table of Contents

  1. Introduction
  2. Basics
  3. Intermediate Concepts
  4. Advanced Topics
  5. Best Practices
  6. Real-World Examples

📚 All exercises and examples from this tutorial are available in the companion repository!
Simply clone it and start coding along: git clone https://github.com/aboubakr-jelloulat/Learn-Makefile
Feel like a system designer - automate everything and let Make handle the heavy lifting! —

Introduction

What is a Makefile?

Makefiles are used to help decide which parts of a large program need to be recompiled. In the vast majority of cases, C or C++ files are compiled. Other languages typically have their own tools that serve a similar purpose as Make. Make can also be used beyond compilation too, when you need a series of instructions to run depending on what files have changed.

Why Do We Use Makefiles?

Without Makefile (The Hard Way)

Let’s say you have a simple C program with 3 files:

1
2
3
4
5
$ gcc -c main.c                             # Compile main.c to main.o
$ gcc -c utils.c                            # Compile utils.c to utils.o  
$ gcc -c math.c                             # Compile math.c to math.o
$ gcc main.o utils.o math.o -o myprogram    # Link all .o files together
$ ./myprogram                               # Run the program

Problems:

  • You have to remember ALL these commands every time
  • You have to TYPE them manually every time
  • If you change ONE file, you might recompile EVERYTHING (very slow)
  • Very easy to make typing mistakes
  • Hard to share your project with teammates
  • No way to easily clean up generated files

WITH MAKEFILE (The Smart Way):

1
2
$ make                                 # That's it! Just one command! 
$ make clean                           # Clean up files

Basics

01 - Hello World

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# This is your very first REAL Makefile.
# A Makefile is like giving instructions to a robot (your computer).
# You tell it what to do when you type `make` in the terminal.

#            **** Variables ****

# CC stands for "C Compiler".
# Here we are telling Make to use `gcc` (GNU Compiler Collection) to compile our C programs.
CC = gcc

# CFLAGS stands for "Compiler Flags".
# These are extra options you pass to the compiler to control how it behaves.
# Common flags include:
# -Wall      : "Warn All"  enables most common warnings to help catch mistakes.
# -Wextra    : enables even more warnings that -Wall does not include.
#              It's useful to catch subtle issues in your code.
# -Werror    : treats all warnings as errors.
#              This means the compiler will stop compiling if any warning exists.
#              It helps enforce clean, warning-free code, which is good practice.
CFLAGS = -Wall

# TARGET is the name of the final executable program we want to create.
# In this case, when we build our program, it will be called "hello".
TARGET = hello

# SOURCE is the name of your source file(s).
# Here we only have one source file: main.c
SOURCE = main.c

#            **** Targets ****

# This is the main target.
# Targets are what Make will try to build.
# The syntax is: target: dependencies
# Then on the next line(s), you give the commands to build it (indented with a tab!)
$(TARGET): $(SOURCE)
# This line tells Make: "To build $(TARGET), use $(CC) to compile $(SOURCE) 
# with the flags $(CFLAGS), and output (-o) a file named $(TARGET). in This case hello
	$(CC) $(CFLAGS) $(SOURCE) -o $(TARGET)

# This is a special target called 'clean'.
# It's not a program, but a command we can run to delete generated files.
# 'rm -f' means "remove the file if it exists, and don't ask for confirmation
# This is useful to start fresh before rebuilding your program.
clean:
	rm -f $(TARGET)

# .PHONY tells Make that 'clean' is not a real file.
# This prevents conflicts in case a file named 'clean' exists in the directory.
.PHONY: clean

# How to use this Makefile:
 #  1 - compile your program :  make 
 #  2 - run your program :  ./hello
 #  3 - To clean up (delete the executable):  make clean

# Reminder : 
        # Don't worry if you don't understand everything yet
        # I'm here to guide you step by step until you become an expert
        
        # Use these resources if you get stuck:
            #    - Ask AI for help
            #    - https://makefiletutorial.com/#top
            #    - https://www.gnu.org/software/make/manual/make.html#Simple-Makefile

main.c:

1
2
3
4
5
6
#include <stdio.h>
int main(void)
{
    printf ("Hej ! You just built ur first program using Make!");
    return (0);
}

02 - Target Dependencies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# This Makefile is specifically for - 42 The Network students - 1337 O Nass Lmlah Tahiya!

# Every student working on libft needs to understand the composition of a Makefile.
# As we all know, during correction with peers, there are always a ton of questions like:
#   - Why is this here?
#   - What happens if we delete that?
#   - Can we do it like this instead? Hahaha
#
# Don't worry  I'm here to guide you step by step and help you make the corrector blessed with your knowledge 

#  ===================== VARIABLES  =====================

# NAME: What are we building? A library called libft.a
NAME = libft.a

# CC: Which compiler should we use?
# WHY USE THIS?
#   - Different systems might have different compilers (gcc, clang, cc)
#   - Easy to switch compilers by changing one line
# CAN WE DELETE THIS?
#   - Yes! Make has a default CC variable set to "cc" - the corrector WILL 100% ask you this don't forget
#   - What happens? Make will use the system default compiler (usually works fine)
CC = cc

# CFLAGS: Compiler flags (extra instructions for the compiler)
# WHY USE THIS? the corrector WILL 100% ask you this don't forget
#   -Wall: "Show me ALL warnings" (helps catch bugs early)
#   -Wextra: "Show me even MORE warnings" (be extra careful)
#   -Werror: "Treat warnings as ERRORS" (forces you to write clean code)
# CAN WE DELETE THIS?
#   - Yes, but your code might have hidden bugs
#   - What happens? Compiler won't warn you about potential problems
CFLAGS = -Wall -Wextra -Werror

# RM: How to remove files
# WHY USE THIS?
#   - "-f" means "force" - don't complain if file doesn't exist
#   - Makes our clean commands more reliable
# CAN WE DELETE THIS?
#   - Yes, but then you'd type "rm -f" everywhere
RM = rm -f

# =====================   SOURCE FILES SECTION =====================

# SOURCES: List of all our .c files
# WHY USE THIS?
#   - One place to manage all source files
#   - Easy to add/remove files from the build
#   - The "\" at the end means "continue on next line"
# CAN WE DELETE THIS? the corrector WILL 100% ask you this don't forget
#   - No! We need to tell Make which files to compile 
#   - What happens? Make won't know what to build!
SOURCES = main.c \
          utils.c

# OBJECTS: Convert .c files to .o files automatically the corrector WILL 100% ask you this don't forget
# WHY USE THIS MAGIC?
#   - $(SOURCES:.c=.o) means "take SOURCES, replace .c with .o"  
#   - So main.c becomes main.o, utils.c becomes utils.o
#   - This is AUTOMATIC - add a .c file above, get a .o file here!
# CAN WE DELETE THIS?
#   - Yes, but then you'd have to list all .o files manually
#   - What happens? More work for you, easy to forget files
OBJECTS = $(SOURCES:.c=.o)

# ARCHV: How to create a library archive the corrector WILL 100% ask you this don't forget
# WHY USE THIS?
#   - "ar rcs" is the command to create a library (.a file) 
#   - ar = archive tool, r = insert files, c = create, s = write index
# CAN WE DELETE THIS?
#   - Yes, but then you'd type "ar rcs" everywhere
#   - What happens? More typing, and if you need to change the method, you'd
#     have to change it in multiple places
ARCHV = ar rcs

# =====================  RULES SECTION ===================== the corrector WILL 100% ask you this don't forget

# DEFAULT TARGET: What happens when you just type "make"
# WHY USE THIS?
#   - "all" is a conventional name meaning "build everything"
#   - It depends on $(NAME), so it will build our library
# CAN WE DELETE THIS?
#   - Yes, but then typing "make" might not do what you expect
#   - What happens? Make will build the first target it finds
all: $(NAME)

# PATTERN RULE: How to make any .o file from a .c file
# WHY USE THIS MAGIC?
#   - %.o means "any file ending in .o"
#   - %.c means "the corresponding file ending in .c"
#   - $< means "the first dependency" (the .c file)
#   - $@ means "the target" (the .o file)
#   - This ONE rule handles ALL our .c files automatically!
# CAN WE DELETE THIS? the corrector WILL 100% ask you this don't forget
#   - Yes, Make has built-in rules for .c to .o
#   - What happens? Make would use its default rule (might not use your CFLAGS)

#   - %.o: %.c utils.h   : Add utils.h as a dependency: this means if anything changes in the header,
#     Make will recompile the source file automatically. the corrector WILL 100% ask you this don't forget

%.o: %.c utils.h   
	@echo "Compiling $< into $@..."
	$(CC) $(CFLAGS) -c $< -o $@

# LIBRARY BUILD RULE: How to create our library
# WHY DOES THIS WORK?
#   - $(NAME) depends on $(OBJECTS)
#   - If any .o file is missing/outdated, Make will build it first
#   - Then it creates the library from all the .o files
# CAN WE DELETE THE DEPENDENCY? the corrector WILL 100% ask you this don't forget
#   - No! Without $(OBJECTS), Make doesn't know what files the library needs
#   - What happens? Make would try to create an empty library!
$(NAME): $(OBJECTS)
	@echo "Creating library $(NAME)..."
	$(ARCHV) $(NAME) $(OBJECTS)
	@echo "Library $(NAME) created successfully!"

# CLEAN: Remove object files but keep the library
# WHY USE THIS?
#   - Object files (.o) are temporary - we don't need them after building
#   - Saves disk space and keeps directory clean
# CAN WE DELETE THIS?
#   - Yes, but your directory will get cluttered with .o files
#   - What happens? Lots of temporary files pile up over time
clean:
	@echo "Cleaning object files..."
	$(RM) $(OBJECTS)
	@echo "Object files cleaned!"

# FCLEAN: Remove EVERYTHING we built (full clean)
# WHY USE THIS?
#   - Sometimes you want to start completely fresh
#   - Removes both object files AND the final library
#   - "fclean" = "full clean" 
# CAN WE DELETE THE "clean" DEPENDENCY?
#   - Yes, but then you'd have to duplicate the object removal commands
#   - What happens? Code duplication (bad practice)

# - fclean depends on clean: this means that when you run `make fclean`,
#   Make will first execute the `clean` target. Think of it like calling a function.
#   After `clean` finishes, fclean continues to remove the executable
fclean: clean
	@echo "Full clean: removing $(NAME)..."
	$(RM) $(NAME)
	@echo "Everything cleaned!"

# RE: Rebuild everything from scratch
# WHY USE THIS?
#   - "re" = "remake" - a common convention
#   - First does fclean (removes everything), then builds all again
#   - Useful when you want to be 100% sure everything is fresh
# CAN WE DELETE THIS?
#   - Yes, but then you'd have to type "make fclean && make" manually
#   - What happens? More typing for a common operation
re: fclean all
	@echo "Rebuild complete!"

# PHONY: Tell Make these are commands, not files the corrector WILL 100% ask you this don't forget
# WHY USE THIS?
#   - Imagine if someone created a file named "clean" in your directory
#   - Without .PHONY, Make would think "clean is up to date" and not run the clean command
#   - .PHONY says "these are always commands, never files"
# CAN WE DELETE THIS?
#   - Yes, but if files with these names exist, the commands might not run
#   - What happens? Your clean/fclean/re commands might stop working!
.PHONY: all clean fclean re

#        Knowledge Check!

#  TARGETS AND DEPENDENCIES:
#   - A target is what you want to make (like "libft.a" or "clean")
#   - Dependencies are what you need to make it (like .c files for .o files)
#   - Make builds dependencies first, then the target
#
#  AUTOMATIC DEPENDENCY TRACKING:
#   - Make compares timestamps: if dependency is newer, rebuild target
#   - This is WHY Make is fast   it only rebuilds what changed!
#
#  VARIABLES:
#   - Variables make your Makefile flexible and maintainable
#   - Change compiler? Just change CC variable
#   - Add new source file? Just add to SOURCES
#
#  PATTERN RULES:
#   - %.o: %.c means "any .o file depends on corresponding .c file"
#   - One rule handles ALL your source files automatically
#
# Try these commands and watch what happens:
#   make          # Build the library
#   make clean    # Remove object files
#   make fclean   # Remove everything
#   make re       # Clean and rebuild
#   touch utils.c # Update timestamp
#   make          # See how only utils.c gets recompiled!

#  How to test your library with a main program:

    # 1 - make   →  This creates libft.a
    
    # 2 - cc -Wall -Wextra -Werror  main.c -L. -lft -o test →  Compile your main program and link it with the library:
        #      - main.c       → your main source file
        #      - -L.          → tells the compiler to look in the current folder for libraries
        #      - -lft         → links the libft.a library (-l + library name without "lib" and ".a")
        #      - -o test      → names the output executable "test"
    
    # 3 -  ./test → Run your program:

# Reminder : 
        # Don't worry if you don't understand everything yet
        # I'm here to guide you step by step until you become an expert
        
        # Use these resources if you get stuck:
            #    - Ask AI for help
            #    - https://makefiletutorial.com/#top
            #    - https://www.gnu.org/software/make/manual/make.html#Simple-Makefile

03 - Simple Pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# The name of the final program we want to build (megaphone)
NAME 	:= 	megaphone

CC 		:= 	c++

CFLAGS 	:= 	-Wall -Wextra -Werror -std=c++98

# All .cpp files in the folder. Example: if you have main.cpp megaphone.cpp ... , both are included automatically
# Why?
# Saves time: you don't have to list every .cpp manually.
SRCS 	:= 	$(wildcard *.cpp)

# Converts every .cpp file into a .o (object) file.
#   Example: main.cpp → main.o
# Why?
# Object files are compiled once, then linked into the final program
OBJS	:= 	$(SRCS:.cpp=.o)

# Default rule. When you run just make, it will try to build megaphone "build everything"

# What if deleted? Make would build the first target it finds (not always what you want)
all: $(NAME)

# What is this?
#   How to build the final program.

# What if deleted?
#  You'd have no final program.
$(NAME): $(OBJS)
	@$(CC) $(CFLAGS) $(OBJS) -o $(NAME)
	@printf "$(CYAN)\n$(NAME) compiled successfully!\n$(RESET)\n"

# Pattern rule (magic)
#     %.o → any .o file.
#     
#     %.cpp → its matching .cpp source.
#     
#     $< → the source file (main.cpp).
#     
#     $@ → the target file (main.o).
#     
# Why? Handles all .cpp → .o compilations automatically.
#     
# What if deleted?
#     Make would fall back to its default rule, but it might not use your flags

%.o: %.cpp
	@printf "$(CYAN)Compiling $<$(RESET)\n"
	@$(CC) $(CFLAGS) -c $< -o $@

# Removes all .o files.
# 
# Why? Keep your repo clean.
# 
# What if deleted?
#     Your repo fills up with .o files → messy.
clean:
	@printf "$(RED)Removing object files$(RESET)\n"
	@rm -f $(OBJS)

# Full clean.
# Removes object files and the final program.
# 
# Why? Sometimes you want to start fresh.
# 
# What if deleted?
#   You can't "reset everything" easily.
fclean: clean
	@printf "$(RED)Removing $(NAME)$(RESET)\n"
	@rm -f $(NAME)

# Rebuild.
#     Deletes everything, then rebuilds from scratch.
# 
# Why? Common shortcut.
# 
# What if deleted?
#     You'd have to run make fclean && make manually.
re: fclean all

# What is this?
# Tells Make these are commands, not files.
# 
# Why? If a file named clean exists in your folder, Make won't get confused.
# 
# What if deleted?
#   Risk that Make thinks your command is "already done".
.PHONY: all clean fclean re

# colors 
RESET	= \033[0m
RED		= \033[31m
CYAN	= \033[36m

#  Tricks 

# @ hides the command, shows only output.
# 
# := expands immediately (safe & predictable).
# 
# = expands later (can change depending on context).

#     ********     The @ flag in Makefile   ******* 
    
test1:
	echo "Hello"   

#         If you run:  
#             make test1

# output : 
#             echo "Hello"
#             Hello

#        If you add @ before the command: 

test2:
	@echo "Hello"
      
#         If you run:  
#             make test2

# output : 
#             Hello

# So @ = "don't echo the command itself, only run it".
# This makes output cleaner and more user-friendly.

#     ********     Diffrence Between  =:   &&  =   ******* 

A = hello
B = $(A) world
A = hej

test3 :
	@printf "$(B)"
	
    # In Makefiles, with = (recursive assignment), variables are expanded only when they are used, not when they are defined
    
    
# if you wanted B to remember the old value of A (so "hello World"), you must use := (immediate assignment):
A := hello
B := $(A) World
A := Hej

test4 :
	@echo "$(B)"
  
# how to run :
    # make 
    # ./megaphone

# Reminder : 
        # Don't worry if you don't understand everything yet
        # I'm here to guide you step by step until you become an expert
        
        # Use these resources if you get stuck:
            #    - Ask AI for help
            #    - https://makefiletutorial.com/#top
            #    - https://www.gnu.org/software/make/manual/make.html#Simple-Makefile
            #    - https://stackoverflow.com/questions/4879592/whats-the-difference-between-and-in-makefile
            #    - https://unix.stackexchange.com/questions/116547/what-do-and-in-a-makefile-mean

Intermediate Concepts

01 - Create Object Directory

Why Do We Need Object Directory?

Object files (.o) are like LEGO blocks for your program:

  • Separate Compilation: Each .cpp file compiles into its own .o file independently
  • Faster Builds: If you change ONE file, only THAT file gets recompiled (not everything!)
  • Organization: Keeps your source directory clean by putting build artifacts in a separate folder
  • Linking: All .o files get “glued together” at the end to create your final program

Example:

  • You have: main.cpp, utils.cpp, math.cpp
  • You change ONLY utils.cpp
  • Without object files: All 3 files get recompiled (slow!)
  • With object files: Only utils.o gets rebuilt, then linked with existing main.o and math.o (fast!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
NAME    := zombie
CC      := c++
CFLAGS  := -Wall -Wextra -Werror -std=c++98

# The folder where we keep our source files.
# Here "." means the current folder (where the Makefile is).
SRCDIR := .

# The folder where we want to put the compiled object files (.o).
OBJDIR := build

# SRCS will contain all the .cpp files inside SRCDIR.
# wildcard is a function that finds all files matching a pattern.
# Example: ./main.cpp ./shared.cpp 
SRCS := $(wildcard $(SRCDIR)/*.cpp)

# OBJS is the list of object files (.o) created from SRCS.
# The pattern replaces "./name.cpp" with "obj/name.o".
# Example: obj/main.o obj/shared.o 
OBJS := $(SRCS:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)

# The default target. When we type "make", it will build $(NAME).
all: $(NAME)

# Rule to build the final program from the object files.
# This links all .o files together into the executable "zombie".
$(NAME): $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) -o $(NAME)

# Rule to compile each .cpp into a .o file.
# $< means the first dependency (the .cpp file).
# $@ means the target (the .o file).
# Example: c++ -Wall -Wextra -Werror -std=c++98 -c ./main.cpp -o obj/main.o
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp utils.h | $(OBJDIR)
	$(CC) $(CFLAGS) -c $< -o $@

# This creates the obj/ folder if it does not exist.
# Without this, the first build would fail when trying to put files in obj/.
$(OBJDIR):
	@mkdir -p $(OBJDIR)

# "make clean" will remove the obj/ folder and all .o files.
clean:
	@rm -rf $(OBJDIR)

# "make fclean" will do clean AND also remove the final program.
fclean: clean
	@rm -f $(NAME)

# "make re" will do a full rebuild: clean everything then build again.
re: fclean all

# These are phony targets, not files.
# Without this line, if a file named "clean" exists in the folder,
# then "make clean" could do nothing by mistake.
.PHONY: all clean fclean re

02 - Generic Makefile - Dependency Handling

Why Do We Need Dependency Handling?

Imagine this scenario: You have a header file weapon.h that contains important function declarations. Your main.cpp includes this header. You modify weapon.h by adding a new function or changing an existing one.

WITHOUT proper dependency handling:

  • You run make
  • Make sees that main.o exists and is newer than main.cpp
  • Make thinks: “Nothing to do here!” and skips compilation
  • Your program still uses the OLD version of the header
  • Result: Your changes are IGNORED and bugs appear!

WITH proper dependency handling:

  • Make knows that main.o depends on BOTH main.cpp AND weapon.h
  • When weapon.h changes, Make automatically recompiles main.o
  • Your program always uses the LATEST version of everything
  • Result: Everything works as expected!

For more details on this critical issue, check out this Stack Overflow discussion


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
NAME := violence
CXX := c++
CXXFLAGS := -Wall -Wextra -Werror -std=c++98

# OPTIONAL BUILD FLAGS (FEATURE TOGGLES)
# These allow you to enable features by setting environment variables
# Usage: make FEATURE=1

# Optimize for speed: make O3=1
# -Ofast: Maximum optimization (can be aggressive)
ifdef O3
	CXXFLAGS += -Ofast
endif

# Add debug symbols: make DEBUG=1
# -g3: Maximum debug information (for gdb debugging)
ifdef DEBUG
	CXXFLAGS += -g3
endif

# Enable AddressSanitizer: make SAN=1
# -fsanitize=address: Detects memory errors at runtime
# Examples of what it catches:
# - Buffer overflows: accessing array beyond its size
# - Use-after-free: using memory after it's freed
# - Memory leaks: forgetting to free allocated memory
# - Stack overflow: excessive stack usage
ifdef SAN
	CXXFLAGS += -fsanitize=address
endif

# Find all .cpp files in the current directory automatically
# Example: if current dir has main.cpp, HumanA.cpp, HumanB.cpp
# then SRCS becomes main.cpp HumanA.cpp HumanB.cpp
SRCS := $(wildcard *.cpp)

# Directory where object files will be stored (keeps source directory clean)
OBJDIR := obj

# Convert .cpp files to .o files in the obj directory
# This does several transformations:
# 1. $(SRCS:.cpp=.o): Change .cpp extension to .o → main.cpp becomes main.o
# 2. $(notdir ...): Remove directory path → main.o becomes main.o (no change for current dir)
# 3. $(addprefix $(OBJDIR)/, ...): Add obj/ prefix → main.o becomes obj/main.o
#
# Example transformation:
# main.cpp → obj/main.o
# HumanA.cpp → obj/HumanA.o
# HumanB.cpp → obj/HumanB.o
OBJFILES := $(addprefix $(OBJDIR)/,$(notdir $(SRCS:.cpp=.o)))

.PHONY: all clean 

all: $(NAME)

# Main target: links all object files into final executable
$(NAME): $(OBJFILES)
	@$(CXX) $(CXXFLAGS) $(OBJFILES) -o $(NAME)
	@echo Created executable \'$(NAME)\'

clean:
	@$(RM) -rf $(OBJDIR)
	@echo Deleted \'obj/\'

fclean: clean
	@$(RM) -f $(NAME)
	@echo Deleted executable \'$(NAME)\'

# Rebuild
re: fclean all

# DEPENDENCY HANDLING (ADVANCED BUT ESSENTIAL)
# Generate .d dependency files from .o files
# patsubst pattern: %.o → %.d
# Example: obj/main.o → obj/main.d
DEPENDS := $(patsubst %.o,%.d,$(OBJFILES))

# Include all dependency files if they exist
# The dash (-) means "don't error if files don't exist"
# This ensures Make knows about header file dependencies
-include $(DEPENDS)

# Rule to compile .cpp files into .o files
$(OBJDIR)/%.o : %.cpp Makefile
# Create output directory if it doesn't exist
	@mkdir -p $(@D)
# Call the tidy_compilation function with the compile command
# The command includes:
# -MMD: Generate dependency files (.d)
# -MP: Add phony targets for headers (prevents errors if headers are deleted)
# -c: Compile only (don't link)
# $<: Input file (the .cpp file)
# -o $@: Output file (the .o file)
	@$(call tidy_compilation,$(CXX) $(CXXFLAGS) -MMD -MP -c $< -o $@)

# TIDY COMPILATION FUNCTION
# Custom function to display clean compilation messages
define tidy_compilation
	@printf "%s\e[K\n" "$(1)"
	# Execute the actual compilation command (passed as $1)
	@$(1)
	# Use ANSI escape codes to clear the previous line
	# \e[A: Move cursor up one line
	# \e[K: Clear the entire line
	# This creates clean output without showing the long compiler command
	@printf "\e[A\e[K"
endef

# USAGE EXAMPLES:
# make              → Normal build
# make DEBUG=1      → Build with debug symbols
# make O3=1         → Build with maximum optimization  
# make SAN=1        → Build with memory error detection
# make clean        → Remove object files
# make fclean       → Remove everything
# make re           → Rebuild from scratch
# make re SAN=1     → Clean rebuild with sanitizer
# Combined flags example:
# make DEBUG=1 SAN=1 → Debug build with memory checking

Advanced Topics

This section will be expanded in future updates…


Best Practices

This section will be expanded in future updates…


Real-World Examples

This section will be expanded in future updates…

This post is licensed under CC BY 4.0 by the author.