make: How Make Works
1
1 2.3 How 'make' Processes a Makefile
1 ===================================
1
1 By default, 'make' starts with the first target (not targets whose names
1 start with '.'). This is called the "default goal". ("Goals" are the
1 targets that 'make' strives ultimately to update. You can override this
DONTPRINTYET 11 behavior using the command line (⇒Arguments to Specify the Goals
Goals.) or with the '.DEFAULT_GOAL' special variable (*noteOther
1DONTPRINTYET 11 behavior using the command line (⇒Arguments to Specify the Goals
Goals.) or with the '.DEFAULT_GOAL' special variable (⇒Other
Special Variables Special Variables.).
1
1 In the simple example of the previous section, the default goal is to
1 update the executable program 'edit'; therefore, we put that rule first.
1
1 Thus, when you give the command:
1
1 make
1
1 'make' reads the makefile in the current directory and begins by
1 processing the first rule. In the example, this rule is for relinking
1 'edit'; but before 'make' can fully process this rule, it must process
1 the rules for the files that 'edit' depends on, which in this case are
1 the object files. Each of these files is processed according to its own
1 rule. These rules say to update each '.o' file by compiling its source
1 file. The recompilation must be done if the source file, or any of the
1 header files named as prerequisites, is more recent than the object
1 file, or if the object file does not exist.
1
1 The other rules are processed because their targets appear as
1 prerequisites of the goal. If some other rule is not depended on by the
1 goal (or anything it depends on, etc.), that rule is not processed,
1 unless you tell 'make' to do so (with a command such as 'make clean').
1
1 Before recompiling an object file, 'make' considers updating its
1 prerequisites, the source file and header files. This makefile does not
1 specify anything to be done for them--the '.c' and '.h' files are not
1 the targets of any rules--so 'make' does nothing for these files. But
1 'make' would update automatically generated C programs, such as those
1 made by Bison or Yacc, by their own rules at this time.
1
1 After recompiling whichever object files need it, 'make' decides
1 whether to relink 'edit'. This must be done if the file 'edit' does not
1 exist, or if any of the object files are newer than it. If an object
1 file was just recompiled, it is now newer than 'edit', so 'edit' is
1 relinked.
1
1 Thus, if we change the file 'insert.c' and run 'make', 'make' will
1 compile that file to update 'insert.o', and then link 'edit'. If we
1 change the file 'command.h' and run 'make', 'make' will recompile the
1 object files 'kbd.o', 'command.o' and 'files.o' and then link the file
1 'edit'.
1