Make works from a dependency graph defined by rules, each of which has a target, its prerequisites, and a recipe to build it. To decide whether to run a rule, Make compares file modification timestamps: it rebuilds the target if the target doesn't exist or if any of its prerequisites is newer than the target. So if you edit one source file, its timestamp updates, making its object file out of date, so Make recompiles just that object and then anything downstream that depends on it (like relinking the executable), while untouched files, whose objects are still newer than their sources, are skipped. That timestamp comparison is exactly what makes builds incremental: you don't recompile the whole project for a one-line change. The correctness of this depends entirely on the dependency graph being complete, if a target's prerequisites don't list everything it actually depends on (notably headers), Make won't know to rebuild it when those change, producing a stale build. So "what to rebuild" is "anything whose recorded prerequisites are newer than it," which is only as reliable as the declared dependencies.
Debugging & Toolchain · Interview question
How does Make decide what to rebuild?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
The Build Process & Makefiles
Automating compile-and-link and rebuilding only what changed: Make rules (target/prereq/recipe), pattern rules and variables, and auto-generated header dependencies that avoid stale builds.
More The Build Process & Makefiles questions
Why might changing a header file not trigger a rebuild, and how do you fix it?What do the automatic variables $@, $<, and $^ mean, and why use pattern rules?What's the infamous Makefile tab error, and what is .PHONY for?What can go wrong with incremental and parallel builds, and how do you guard against it?
Browse all 472 interview questions