106Chapter4 • Using GNU make
You can specify any other filename using command line switch –
f
with the
make
com-
mand. For example, if you have a file named
myrules.make
, you can use it as input file to the
make
utility as follows:
make –f myrules.make
From now on in this book, we shall use only “Makefile” as the name of the input file for
the
make
command. This is a usual convention in most of the software development projects.
Input files with any other name will be used as include files in the Makefile. The common con-
ventional name for include file is
Makefile.in
.
4.1.3Typical Contents of a Makefile
The makefiles that are used as input to the
make
command have similar types of contents.
Some of the typical contents of a makefile are as follows.
Variables
Variables are usually the first part of any makefile. Variables are used to associate a name
to a text string in the makefiles. These variables are then substituted into other targets, dependen-
cies or commands later in the makefile. A simple equal sign can associate a text string to a name.
The following line defines a variable
targets
in the makefile. This variable is equal to two target
files
tftp.o
and
fto.o
.
targets = tftp.o ftp.o
To use this variable later on, you enclose the variable name in parentheses and use a dollar
sign with it. The targets variable will be used as
$(targets)
in the makefile to refer to these
two object files. A typical use of this variable is shown in the following three lines.
$(targets): tftp.c tftp.h ftp.c ftp.h
gcc –c tftp.c
gcc –c ftp.c
This rule tells
make
command that if any of the four dependency files (
tftp.c,
tftp.h, ftp.c, ftp.h
) has a timestamp newer than the object files, the object files
should be rebuilt.
Now you may have noticed that this is not a wise rule. If only
tftp.c
is changed, then
we need to build only
tftp.o
but with this rule we are also building
ftp.o
which may not be
required. However this type of rule may be useful when a single dependency may affect many
targets. For example, both
ftp.o
and
tftp.o
may be dependent on
common.h
file and the
following rule will make sense.
$(targets): common.h
gcc –c tftp.c
gcc –c ftp.c
We shall discuss more about these things and how to write wiser and more comprehensive
rules in makefiles later in this chapter.
Next Page >>
<< Previous Page
Back to the Table of Contents