Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 493
makeMonday Avatar asked Mar 25 '14 09:03

makeMonday


People also ask

How do I recompile with makefile?

Use the command `make' to recompile the source files that really need recompilation. Make the changes in the header files. Use the command `make -t' to mark all the object files as up to date. The next time you run make, the changes in the header files do not cause any recompilation.

How does make know that a source code file needs to be recompiled?

How does make know that a source code file needs to be recompiled? Freshness Check: Speed is the reason we use make. For maximum speed, make does the least amount of work to determine the freshness of the compiled object code.

How does work a makefile?

The make utility requires a file, Makefile (or makefile ), which defines set of tasks to be executed. You may have used make to compile a program from source code. Most open source projects use make to compile a final executable binary, which can then be installed using make install .

What is the relationship between make and makefile?

The makefile is read by the make command, which determines the target file or files that are to be made and then compares the dates and times of the source files to decide which rules need to be invoked to construct the target.


2 Answers

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

like image 154
Clifford Avatar answered Oct 10 '22 05:10

Clifford


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.

like image 43
david.pfx Avatar answered Oct 10 '22 05:10

david.pfx