Incremental builds and file timestamps
What you'll learn: How make uses file timestamps to save time by only recompiling the parts of your code that have changed.
The "Smart" Chef Analogy
Imagine you are a chef making a complex three-course meal. You have already chopped the salad, simmered the soup, and seasoned the steak. Suddenly, you realize you want to add more salt to the soup.
A "dumb" chef would throw everything away and start the entire meal from scratch—re-chopping the salad and re-seasoning a new steak. A "smart" chef (like make) looks at the kitchen and asks: "When was each dish last touched?" Since the salad and steak were finished and haven't changed, the chef only re-heats and fixes the soup. This is the essence of an incremental build.
Timestamps: The Secret Sauce
In C programming, make doesn't actually read your code to see what you changed. Instead, it looks at the timestamps—the date and time recorded by your operating system showing when a file was last modified.
When you run make, it compares two things:
- The timestamp of your source file (
main.c). - The timestamp of your compiled file (
main.oor the final app).
If the source file is "newer" than the compiled file, make assumes you made an edit and triggers a recompile. If the compiled file is newer, make knows the work is already done and simply skips that file.
Seeing it in Action
Consider a project with two separate files. If you only change utils.c, make is smart enough to leave main.c alone.
// main.c - Last modified: 10:00 AM
// utils.c - Last modified: 10:30 AM (You just fixed a bug here!)
// If your program 'my_app' was last built at 10:00 AM...
// 'make' sees that 'utils.c' is newer than 'my_app'.
// It will recompile 'utils.c' but skip 'main.c' entirely.
In a small project with two files, the time saved is milliseconds. But in professional software with thousands of files, this "incremental" approach turns a three-hour wait into a three-second update. By relying on timestamps, make ensures that your build process is as efficient as possible, doing the bare minimum amount of work to get the job done.
Key takeaway
make uses file timestamps to perform incremental builds, skipping files that haven't changed to save time.