Using Variables121
instead of just the
=
symbol. If we change the
OBJS
line in the above makefile to this type of
variable, the value printed by the makefile will be as follows:
[root@conformix make]# make
ftp.o common.o
[root@conformix make]#
Now
make
did not take into consideration the changed value of the
OBJ1
variable later in
the makefile.
There are advantages and disadvantages to both types of variables. You can decide which
type of variable to use in a particular situation.
4.3.3Pre-Defined Variables
The
make
utility can also take variables from the shell environment. For example, the
CFLAGS
variable can be set through shell startup file (e.g.
/etc/profile
). If this variable is
not redefined inside the makefile, its value will be taken from the environment.
4.3.4Automatic Variables
Some variables are pre-defined and are called
automatic variables
. They are usually very
short in length but play a very important role in the decision-making process. The most com-
monly used automatic variables are listed below.
•The
$@
variable contains the value of the target of a rule.
•The
$<
variable always contains the first dependency of a rule.
•The
$.
variable contains a list of modified files in the dependency list. If the target is
being built for the first time, it contains a list of all dependencies. Consider the
following makefile.
# Variable definition
OBJS = ftp.o common.o
HDRS = ftp.h common.h
CFLAGS = -g -O2
TARGETS = ftp
CC = gcc
# Default Target
ftp: $(OBJS) $(HDRS)
@echo $.
@echo $@
@echo $<
$(CC) $(OBJS) -o ftp