104Chapter4 • Using GNU make
Target
A target is usually a file or set of files that is the result of some operation on one or more
other files. A target may be an object file, a final executable file or some intermediate file. Tar-
gets may be fake or phony, in which case no files are actually rebuilt. One such example is the
phony target to remove all object and executable files from the source files directory. This pro-
cess is often called a
cleaning process
. As a convention, all makefiles have a rule to clean the
source code so that everything can be rebuilt freshly.
Dependency
The dependencies are files that determine when to build a target. The decision to rebuild a
target is made if the timestamp on any dependency is newer than the target. This usually shows
that a dependency file has been modified after the target was built the last time. Consider the
example of an object file that was built from a C source file. After building the object file, if
someone modifies the source code file, we need to rebuild the object file because it is no longer
current. When the
make
utility scans the input files, it verifies this rule and rebuilds the object
file automatically if it finds a change in the source file. If multiple machines are used, they need
to be in time sync.
A target may have multiple dependencies separated by space characters. In case of multi-
ple dependencies, the target is rebuilt if any of the dependencies has changed. If the list of
dependencies is very long, you can write multiple lines for dependencies by using a backslash
character at the end of a line to continue to the next line. For example, the following lines show
that
tftp.o
has dependencies spanning three lines.
tftp.o: tftp.c tftp.h file1.c file2.c file3.c file4.c\
file5.c file6.c file7.c file1.h file2.h file3.h\
file4.h file5.h
Note that the starting character in the second and third line should not be a TAB character;
otherwise
make
will consider these lines as command lines and will try to execute these.
Default Goal
An input file (makefile) usually contains many rules. Each of these rules has a target.
However the
make
utility does not build all of these targets. It only tries to build the first target
specified in the input makefile. This target is called the
default
goal of the makefile. However,
note that if dependencies to this default goal are targets of some other rules, the
make
utility can
also build those targets in an effort to build the default goal. This happens when dependencies of
the default goal are out of date.
If you want to build a target that is not the default goal, you have to specify the target at the
command line when starting
make
. This will be explained later in this chapter.
Phony Target
Some targets may have no dependencies. A common example is the
clean
target that
removes some files from the source directory tree. A typical
clean
target is shown below: