77

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?

2 Answers 2

85

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

5
  • Just one more thing... It handles those changes like, for instance, git does? Is it possible to read that file? Thanks :)
    – makeMonday
    Mar 25, 2014 at 11:43
  • @makeMonday: I don't understand your question. What makefile does is exactly what Clifford said in your answer. Things like keep a copy of previously file in order to compare with a more recent isn't part of Make job.
    – The Mask
    Mar 28, 2014 at 5:37
  • 1
    @TheMask Yes, now I totally understand. I read about those time-stamps. I was just wondering how they worked. Thanks ;)
    – makeMonday
    Mar 28, 2014 at 7:43
  • @syam : The timestamp is determined by the filesystem/OS, not by make.
    – Clifford
    Jul 21, 2017 at 18:43
  • 3
    @user13267 : If you have a question, post a question; this answer is three years old, and SO is not a discussion forum. You could answer your own question by performing exactly that experiment. Whether it would recompile depends on the timestamp of the target file relative to the source file through a chain of dependencies. The day before is not the issue; making it older than the target is what causes the rule to be invoked (which need not be compilation; it can be any command). make has a debug option which outputs verbose information about how it is processing the rules.
    – Clifford
    Jul 27, 2017 at 9:38
38

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.