make: Simple Makefile

1 
1 2.2 A Simple Makefile
1 =====================
1 
1 Here is a straightforward makefile that describes the way an executable
1 file called 'edit' depends on eight object files which, in turn, depend
1 on eight C source and three header files.
1 
1    In this example, all the C files include 'defs.h', but only those
1 defining editing commands include 'command.h', and only low level files
1 that change the editor buffer include 'buffer.h'.
1 
1      edit : main.o kbd.o command.o display.o \
1             insert.o search.o files.o utils.o
1              cc -o edit main.o kbd.o command.o display.o \
1                         insert.o search.o files.o utils.o
1 
1      main.o : main.c defs.h
1              cc -c main.c
1      kbd.o : kbd.c defs.h command.h
1              cc -c kbd.c
1      command.o : command.c defs.h command.h
1              cc -c command.c
1      display.o : display.c defs.h buffer.h
1              cc -c display.c
1      insert.o : insert.c defs.h buffer.h
1              cc -c insert.c
1      search.o : search.c defs.h buffer.h
1              cc -c search.c
1      files.o : files.c defs.h buffer.h command.h
1              cc -c files.c
1      utils.o : utils.c defs.h
1              cc -c utils.c
1      clean :
1              rm edit main.o kbd.o command.o display.o \
1                 insert.o search.o files.o utils.o
1 
1 We split each long line into two lines using backslash/newline; this is
11 like using one long line, but is easier to read.  ⇒Splitting Long
 Lines Splitting Lines.
1 
1    To use this makefile to create the executable file called 'edit',
1 type:
1 
1      make
1 
1    To use this makefile to delete the executable file and all the object
1 files from the directory, type:
1 
1      make clean
1 
1    In the example makefile, the targets include the executable file
1 'edit', and the object files 'main.o' and 'kbd.o'.  The prerequisites
1 are files such as 'main.c' and 'defs.h'.  In fact, each '.o' file is
1 both a target and a prerequisite.  Recipes include 'cc -c main.c' and
1 'cc -c kbd.c'.
1 
1    When a target is a file, it needs to be recompiled or relinked if any
1 of its prerequisites change.  In addition, any prerequisites that are
1 themselves automatically generated should be updated first.  In this
1 example, 'edit' depends on each of the eight object files; the object
1 file 'main.o' depends on the source file 'main.c' and on the header file
1 'defs.h'.
1 
1    A recipe may follow each line that contains a target and
1 prerequisites.  These recipes say how to update the target file.  A tab
1 character (or whatever character is specified by the '.RECIPEPREFIX'
1 variable; ⇒Special Variables) must come at the beginning of every
1 line in the recipe to distinguish recipes from other lines in the
1 makefile.  (Bear in mind that 'make' does not know anything about how
1 the recipes work.  It is up to you to supply recipes that will update
1 the target file properly.  All 'make' does is execute the recipe you
1 have specified when the target file needs to be updated.)
1 
1    The target 'clean' is not a file, but merely the name of an action.
1 Since you normally do not want to carry out the actions in this rule,
1 'clean' is not a prerequisite of any other rule.  Consequently, 'make'
1 never does anything with it unless you tell it specifically.  Note that
1 this rule not only is not a prerequisite, it also does not have any
1 prerequisites, so the only purpose of the rule is to run the specified
1 recipe.  Targets that do not refer to files but are just actions are
1 called "phony targets".  ⇒Phony Targets, for information about
1 this kind of target.  ⇒Errors in Recipes Errors, to see how to
1 cause 'make' to ignore errors from 'rm' or any other command.
1