Reputation: 983
I'm learning GNU make
. Suppose I have a hello_world.c
file and a Makefile
:
hello_world.c:
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
Makefile:
hello: hello_world.c
gcc hello_world.c -o hello_world
Now, I think hello
is my target and hello_world.c
is its dependency. If make somehow detects that hello_world.c
is newer than its object-file, it executes the corresponding command.
1- How does make
manage file version control and how does it detect something is newer than something else and needs updating?
2- If I change hello_world.o
using an editor and corrupt the file, it obviously does not execute but make hello
reports that nothing needs to be done!
I mean, make
only checked that the dependency is older than the target and exited doing nothing. I think it should have detected that this target is not the one corresponding to its latest invocation. Somehow, it should have compared the "combination of dependency AND target" history instead of just comparing dependency's history w.r.t. target.
3- Is this the limitation of make
? How can I circumvent this issue? Because there maybe some external apps messing with the target of my make operation.
Upvotes: 0
Views: 700
Reputation: 206667
1- How does
make
manage file version control and how does it detect something is newer than something else and needs updating?
You already know the answer to this question. You said it in your next paragraph: "I mean, make
only checked that the dependency is older than the target and exited doing nothing." That's right. make
updates dependent targets when the dependencies of targets are newer.
2- If I change
hello_world.o
using an editor and corrupt the file, it obviously does not execute butmake hello
reports that nothing needs to be done! I mean,make
only checked that the dependency is older than the target and exited doing nothing. I think it should have detected that this target is not the one corresponding to its latest invocation. Somehow, it should have compared the "combination of dependency AND target" history instead of just comparing dependency's history w.r.t. target.
You are asking make
to do a lot more than what it was intended to do.
3- Is this the limitation of
make
? How can I circumvent this issue? Because there maybe some external apps messing with the target of my make operation.
From your point of view, it does seem like it is a limitation of make
. However, I want to point out that you are sabotaging the workings of make
by updating a target manually.
How can you circumvent it?
Don't manually modify targets that are built by make
.
Manually update the time stamp of one of the dependencies and then run make
. You can use the command touch
for that.
Provide a dummy target named clean
that will remove all the dependent targets. Then, run make clean
followed by make
.
Provide a dummy target named rebuild
. Forcefully build whatever you need to build in that target. Then, run make rebuild
.
Upvotes: 1
Reputation: 12404
How does make manage file version control and how does it detect something is newer than something else and needs updating?
Very simple: make
does not care about file versions.
Detecting if something is newer than something else is simply done by comparing time stamps from file system.
If I change hello_world.o using an editor and corrupt the file, it obviously does not execute but make hello reports that nothing needs to be done! I mean, make only checked that the dependency is older than the target and exited doing nothing. I think it should have detected that this target is not the one corresponding to its latest invocation. Somehow, it should have compared the "combination of dependency AND target" history instead of just comparing dependency's history w.r.t. target.
You did not tell make
anything about your .o file. Why should make
check the timestamp of that file?
Checking the dependency is exactly what make
is expected to do.
How should this tool possibly know that a file calles hello_world.o
is involved in the process if you don't tell it? There is no magic happening but only the rules of your Makefile are followed.
Is this the limitation of make? How can I circumvent this issue? Because there maybe some external apps messing with the target of my make operation.
You can specify hierarchical dependencies:
all: hello
hello: hello_world.o
<gcc linker command...>
hello_world.o: hello_world.c
gcc hello_world.c -o hello_world
Upvotes: 1
Reputation: 409346
The make
program simply compare the modification time-stamps of the target (hello
) and the dependency (hello_world.c
) files.
If the time-stamp of a dependency file is newer than the targets, then it execute the commands.
Upvotes: 1