How does make know which files to update

c, compilation, gcc, makefile

Solution

It inspects the file system's modification date meta information.

See, for instance, the stat() man page and the `st_mtime` member of the `struct stat`.

It has built-in rules that tells it that (for instance) a .o file needs to be re-generated if the corresponding .c file has changed; the manual section on rule syntax says:

The criterion for being out of date is specified in terms of the prerequisites, which consist of file names separated by spaces. (Wildcards and archive members (see Archives) are allowed here too.) A target is out of date if it does not exist or if it is older than any of the prerequisites (by comparison of last-modification times).

Problem

I noticed that when I make changes to some files and then I type make, it will run certain commands related to those files. If I don't change anything, then make doesn't do anything, saying that the program is up to date. This tells me that make has a way of knowing which files were changed since it was last run. How does it know? It doesn't seem to put anything in the directory where it is run, so it must be storing this information somewhere else.

Original source