How does Makefile know that a file changed and then recompile it?

MakefileProgramming Languages

Makefile Problem Overview


Just out of curiosity, how does Makefile know that a file changed (and then recompile it)? Is it up to make? Is it up to the compiler? If so, is it language dependent?

Makefile Solutions


Solution 1 - Makefile

It looks at the file time-stamp - simple as that. If a dependency is newer that the target, the target is rebuilt.

Solution 2 - Makefile

Make works by inspecting information about files, not their contents.

Make works out dependencies between targets and their dependencies, and then looks to see whether the files exist. If they do, it asks the operating system for the time and date the file was last modified. This is the 'timestamp' for this purpose, although the term can have other meanings.

If a target file either does not exist, or exists and is earlier than its dependent file, then Make rebuilds the target from the dependent by applying a rule.

If the dependent does not exist, Make signals an error.

A consequence of this is that you can force a rebuild by deleting the target, or by 'touching' the dependent to make it later than the target. You can avoid a rebuild by 'touching' the target. Touching simply updates the timestamp to now.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmakeMondayView Question on Stackoverflow
Solution 1 - MakefileCliffordView Answer on Stackoverflow
Solution 2 - Makefiledavid.pfxView Answer on Stackoverflow