CST8204: Lab Exercise 4 -- The make Utility

The Linux utility make will simplify the compiling and linking of a program that has its source separated into several files. make is a general rule-based utility than can be used for many tasks. For this example, assume that you have a C programming project that is made up of three separate C source files (one with main(); two with each of the other functions) and one common #include file: main.c, partA.c, partB.c, and myProg.h

To recompile this entire program you would normally type a command like this:

gcc -ansi -pedantic -Wall -Wextra -O2 -o myProg main.c partA.c partB.c

This would cause all source files to be recompiled every time, even if only a single file has been modified, a distinct disadvantage for a large program.

A better way is to let the make utility manage the recompilation of only those files that have changed. To do this, create a rule file, typically named Makefile, that defines the commands needed to handle the compilation and linking phases in separate steps, with individual compilations for each source file. See the Preparation document for some further information.



Construct a Makefile and a simple test program (have each function display a message, for example) similar to the one above that will compile and link just by typing the command make (the program will assume your rules are in Makefile in the current directory) and demo both to your instructor. Before the end of the lab