Phony Targets:: Clean: RM .O Temp RM Make Clean
Phony Targets:: Clean: RM .O Temp RM Make Clean
Phony Targets:
A phony target is one that is not really the name of a file; rather it is just a name for a
recipe to be executed when you make an explicit request. There are two reasons to
use a phony target:
1) To avoid a conflict with a file of the same name, and
2) To improve performance.
If you write a rule whose recipe will not create the target file, the recipe will be
executed every time the target comes up for remaking. Here is an example:
clean:
rm *.o temp
Because the rm command does not create a file named clean, probably no such file
will ever exist. Therefore, the rm command will be executed every time you say
‘make clean’.
In this example, the clean target will not work properly if a file named clean is ever
created in this directory. Since it has no prerequisites, clean would always be
considered up to date and its recipe would not be executed. To avoid this problem
you can explicitly declare the target to be phony by making it a prerequisite of the
special target .PHONY
For example:
.PHONY: clean
clean:
rm *.o temp
Once this is done, ‘make clean’ will run the recipe regardless of whether there is a
file named clean.
Phony targets are also useful in conjunction with recursive invocations of make.
if you want a dollar sign to appear in your recipe, you must double it (‘$$’). For shells
like the default shell, that use dollar signs to introduce variables, it’s important to
keep clear in your mind whether the variable you want to reference is a make
variable (use a single dollar sign) or a shell variable (use two dollar signs).
For example:
LIST = one two three
print:
for i in $(LIST); do \
echo $$i; \
done
#Command:
# make print
#Output:
# one
# two
# three
Variable references work by strict textual substitution.
$%
The target member name, when the target is an archive member. For example, if the
target is foo.a(bar.o) then ‘$%’ is bar.o and ‘$@’ is foo.a.
‘$%’ is empty when the target is not an archive member.
$<
The name of the first prerequisite.
If the target got its recipe from an implicit rule, this will be the first prerequisite added
by the implicit rule.
$?
The names of all the prerequisites that are newer than the target, with spaces
between them.
For prerequisites which are archive members, only the named member is used.
$^
The names of all the prerequisites, with spaces between them.
For prerequisites which are archive members, only the named member is used.
A target has only one prerequisite on each other file it depends on, no matter how
many times each file is listed as a prerequisite. So if you list a prerequisite more than
once for a target, the value of $^ contains just one copy of the name.
This list does not contain any of the order-only prerequisites; for those see the ‘$|’
variable, below.
$+
This is like ‘$^’, but prerequisites listed more than once are duplicated in the order
they were listed in the makefile. This is primarily useful for use in linking commands
where it is meaningful to repeat library file names in a particular order.
$|
The names of all the order-only prerequisites, with spaces between them.
S(MAKE)
The value of this variable is the file name of Makefile.
For Example:
.PHONY:create_dir
create_dir:
mkdir -p dir
.PHONY:create
create:
touch file.txt
$(MAKE) create_dir
#Command:
# make create
#Output:
# Directory named "dir" and File named "file.txt" will be created
is equivalent to
bigoutput : text.g
generate text.g -big > bigoutput
littleoutput : text.g
generate text.g -little > littleoutput