# # This Makefile illustrates common rule formatting and some common shortcuts. # # # Definitions at the top of a Makefile are used to give shorter names to # longer UNIX commands, or parts of UNIX commands. The two definitions in this # Makefile name the compiler arguments we use, and the particular C compiler we # use. The names "CFLAGS" and "CC" could be any names we want; these # particular names are standard practice in UNIX Makefiles. # CFLAGS = -ansi -pedantic -Wall -Werror -lm CC = gcc $(CFLAGS) # # The rule named "all" compiles all eight example stats programs. It does this # by depending on the eight separate rules defined below. # # Note the backslash at the end of the first and second lines of the rule. # This is just a line-continuation character, that effectively makes the rule # one line long. The syntax of a Makefile requires that the first line of a # rule have the rule name and all of its dependencies. Therefore, if it's # longer than 80 characters, it's common practice to break it up using # backslash line continuation characters. This is simply a formatting nicety; # lines longer than 80 characters are entirely acceptable in a Makefile. # all: stats-wrong stats-no-functions stats-with-functions \ stats-further-refined stats-non-neg stats-neg-drop stats-neg-error \ stats-neg-error-return # # The stats-wrong rule depends on the C program stats-wrong.c. It compiles # this program using the CC shortcut defined at the top of the Makefile. To # use a shortcut, wrap its name in $(...). For this rule, the shortcut '$(CC)' # is equivalent to 'gcc -ansi -pedantic -Wall -Werror'. # stats-wrong: stats-wrong.c $(CC) stats-wrong.c -o stats-wrong # # The seven other rules have the same format at stats-no-functions. # stats-no-functions: stats-no-functions.c $(CC) stats-no-functions.c -o stats-no-functions stats-with-functions: stats-with-functions.c $(CC) stats-with-functions.c -o stats-with-functions stats-further-refined: stats-further-refined.c $(CC) stats-further-refined.c -o stats-further-refined stats-non-neg: stats-non-neg.c $(CC) stats-non-neg.c -o stats-non-neg stats-neg-drop: stats-neg-drop.c $(CC) stats-neg-drop.c -o stats-neg-drop stats-neg-error: stats-neg-error.c $(CC) stats-neg-error.c -o stats-neg-error stats-neg-error-return: stats-neg-error-return.c $(CC) stats-neg-error-return.c -o stats-neg-error-return # # A rule named "clean" is commonly used in Makefiles. It has no dependencies. # It unconditionally runs the UNIX rm command to remove all of the compiled # programs, so we can re-compile them all fresh. To run this rule from the # UNIX command line, type the command make clean. # # Note that this rule is longer than 80 characters on one line, just to # illustrate it's OK to do this. I.e., it doesn't have backslash line # continuations, like the all rule above. # clean: rm -f stats-wrong stats-no-functions stats-with-functions stats-further-refined stats-non-neg stats-neg-drop stats-neg-error stats-neg-error-return