Chapter 2. Basic Principles of the Tools 9
target is usually the name of a file that a program generates; examples of targets are executable or
object files. A target can also be the name of an action to carry out, such as with the clean command (a
command that, deletes all files from a build directory before building). A dependency is a file that
is used as input to create the target. A target often depends on several files. command is activated by
make wnen dependancy has changed. A rule may have more than one command, with each command
on its own line. Usually a command is in a rule with dependencies and serves to create a target file if
any dependencies change. However, the rule that specifies commands for the target does not require
dependencies; for instance, the rule containing the delete command that associates with the target,
clean, does not have dependencies. make activates commands on the dependencies to create or to
update the target. A rule can also explain how and when to activate a command. A makefile can
contain other text besides rules; a simple makefile requires only rules. Rules generally follow the
same pattern.
Note that every command line in a makefile must begin with a tab character.
2.10.1. Example Makefile
edit : main.o kbd.o command.o display.o insert.o search.o \
files.o utils.o
cc -o edit main.o kbd.o command.o display.o insert.o \
search.o files.o utils.o
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm edit main.o kbd.o command.o display.o insert.o \
search.o files.o utils.o
The targets in the example makefile include the executable file, edit, and the main.o and kbd.o
object files. main.c and defs.h are the dependency files. Each .o file is both a target and a depen-
dency. When a target is a file, it needs to be recompiled or relinked if any of its dependencies change.
You must first update any dependencies that automatically generate. In the example Makefile, edit
depends on eight object files; the object file, main.o, depends on the source file, main.c, and on
the defs.h header file. A shell command follows each line that contains a target and dependencies,
saying how to update the target file; a tab character must come at the beginning of every command line
to distinguish command lines from other lines in the makefile. make does not know anything about
Komentarze do niniejszej Instrukcji