# Introducing Makefiles: Rules
# Written by Stewart Weiss
# September 10, 2007
# Revised Feb. 2, 2012
# Revised March 19, 2017

# A makefile consists of “rules” of the form
#
#     TARGET ... : PREREQUISITE ...
#     <tab char> RECIPE 
#
# A TARGET is usually the name of a file that is generated by a program, such as
# an executable or an object file. The "..." above just means 
# "one or more of the preceding things."
#
# A PREREQUISITE is a file that is used as input to create the TARGET. 
# It is best to think of a PREREQUISITE as something upon which the TARGET 
# depends, so that if the PREREQUISITE is changed in any way, the TARGET might
# be changed as well.
#
# A RECIPE is an action that make carries out. A RECIPE may consist of 
# more than one command, either on the same line or on separate lines. 
# Note that there MUST BE a tab character preceding the RECIPE.
#
# Usually a RECIPE is part of a rule with prerequisites and is used to create 
# a TARGET file if any of the prerequisites change. 
# Sometimes a RECIPE may be part of a rule that has no prerequisites.
#
# The remainder of this makefile consists of rules used to build a program 
# named lesson from the source code files in this directory. To create
# the program named lesson, one types on the command line
#
#     make
#
# which causes the make program to read this makefile and follow the instructions
# in it. 
#
# The first rule below states that lesson depends on three object files,
#     main.o, utils.o, and interface.o.
# If any of them "change", then the recipe 
#     "gcc -o lesson main.o utils.o interface.o"
# will be executed.  The word "change" is in quotes because make does not
# check whether a file has actually changed; it checks whether the file's time 
# of last modification is later than the time of last modification of the target
# that depends upon it.
#
# The next three rules say how each of the object files is built.
# They each have the same form: an object file named X.o depends on a source
# code file named X.c  and possibly some ".h" files.
# Each is built with a RECIPE of the form "gcc -c X.c". For example,
# main.o depends on two header files and main.c. If either header file is
# changed or if main() itself changes, then main.o must be regenerated. Note 
# that the command to regenerate it depends only on main.c, not the header files!
#
# make works recursively: if lesson needs to be updated because it is older than 
# one of its prerequisites, then make will also run the rules for each of lesson's 
# prerequisites to determine whether any of them need to be rebuilt. 
# It looks at the rules of their prerequisites in the same way, and so on.


lesson: main.o utils.o interface.o
	gcc -o lesson main.o utils.o interface.o

main.o: main.c utils.h interface.h
	gcc -c main.c

utils.o: utils.c utils.h defs.h
	gcc -c utils.c

interface.o: interface.c interface.h defs.h
	gcc -c interface.c

