0

I have a following Makefile:

$ cat Makefile
all: foo

foo: bar
        @true

bar: file.txt
        touch file.txt

file.txt:
        @echo 'Created file.txt'

run:
        @echo 'Target without dependency.'
$

When I execute make, then touch file.txt is always executed even when it already exists. Why is that so? I expected bar target not to run if dependency is met, i.e file.txt exists. In addition, am I correct that make by default picks the first target of the Makefile which is named all in my example?

2

First of all, make does not care what the commands are doing; it just passes them to the shell.

Second, since you're never creating any bar file, file.txt will always be newer than bar, the bar: file.txt rule will match, and the touch file.txt command will be run.

You should obviously touch or create file.txt in the rule which has file.txt as its target.

In addition, am I correct that make by default picks the first target of the Makefile which is named all in my example?

Yes.

| improve this answer | |
2

Your Makefile is not using any non-standard feature, so your question is not specific to GNU make.

  • The first regular target in the makefile is all, wich makes it the so called default target.

  • Since I assume that no file, called "all" exist in the filesystem and since the makefile does not create one, all can be seen as a pseudo target that will always considered out of date.

  • all depends on foo that also is not created by the makefile and thus also always considered out of date.

  • foo depends on bar and when foo is "created" (from the view of the makefile), the dummy command true is executed...but not a file named foo. So foo is also always considered out of date.

  • bar depends on file.txt and when bar is "created" (from the view of the makefile), the command touch file.txt is executed. This makes file.txt more recent and if a file bar did exists, this would actively make bar out of date with repect to file.txt, since bar would no longer be younger than file.txt.

  • The command to make file.txt recent is just an echo command, that does not have any effect on file.txt but since any make implementation assumes a special date younger than any file (without checking the actual presence or time stamp of the target file) after a command for a target has been run, this does not cause the make run to fail.

So your makefile does not make or remake file.txt in the rule for file.txt.

On the other side, your makefile makes file.txt actively out of date with repect to bar when bar is expected to be processd. Note that bar depends onfile.txt.

I recommend you to start with simpler makefiles to get a feeling on how make works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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