Difference between revisions of "Makefile"

From Ever changing code
Jump to navigation Jump to search
Line 13: Line 13:
cat ./clean_runfile
cat ./clean_runfile
EOF
EOF
</source>
Running the example:
<source lang=bash>
make clean # executes the commands explicitly as it has .PHONY target
cat ./clean
echo "This is clean file"
make clean_runfile
make: 'clean_runfile' is up to date.
piotr@Sylwia-zonka:~/src/myprojects/Makfile_adventures$
</source>
</source>

Revision as of 12:21, 1 October 2021

Makefile adventures

.PHONY target

By default, Makefile targets are "file targets" - they are used to build files from other files. Make assumes its target is a file. These special targets are called phony and you can explicitly tell Make they're not associated with files. We don't need to use .PHONY as long as you don't have a file with the same name as the task. The task will always be executed anyway, and the Makefile will be more readable.

echo "echo \"This is clean file\""         > clean
echo "echo \"This is clean_runfile file\"" > clean_runfile

cat > Makefile <<EOF
.PHONY: clean
clean:
	cat ./clean
clean_runfile:
	cat ./clean_runfile
EOF

Running the example:

make clean # executes the commands explicitly as it has .PHONY target
cat ./clean
echo "This is clean file"

make clean_runfile 
make: 'clean_runfile' is up to date.
piotr@Sylwia-zonka:~/src/myprojects/Makfile_adventures$