Search code examples
makefilegnu-make

Why doesnt make create file for the target " other_file" in below example?


I am new to make utility and I was going through a tutorial. In the tutorial, they gave an example where every time we run make on it, the targets are executed because target file is not created.
My question is why isn't target file created in this example? As per my understanding, when the commands are executed, the target file gets created. Next time when make runs on the file, it checks whether target file is present or not. If present, then it doesn't execute the commands again.

I am using GNU make 4.3 on debian.

some_file: other_file
    echo "This will always run, and runs second"
    touch some_file

other_file:
    echo "This will always run, and runs first"

the above always executed in that order because some_file depends on other_file which is never created. Why isn't other_file file created?


Solution

  • As per my understanding, when the commands are executed, the target file gets created.

    Not necessarily. When the commands for a target are executed, the result is whatever effect those commands have. Often it is desired that they should create a file with the same name as the target, but that's not automatic. If that's what the makefile author intends then it is the author's responsibility to provide a recipe that achieves it.

    In this regard, note a key difference between your recipe for some_file and your recipe for other_file: the former contains the command touch some_file, which will create that file if necessary and set its last modification timestamp in any case. The latter contains no such command.

    So,

    Why isn't other_file file created?

    Because the recipe for other_file does not have that effect.