CIS 22 - Data Structures: Unix makefile utility


When you are working on a large program, it can be difficult, time consuming, and tedious to determine which modules need to be recompiled due to their dependency relationships. The make utility automates this process.

In order to use make, the users must specify dependencies in a description file. The default description filenames are makefile or Makefile. When the user types make, the make utility looks for makefile or Makefile in the current directory and checks this description file if anything needs updating.

The description file describes the dependency relationships that exist between various program modules. The dependencies in the description file have the form

target:                  components
TAB                     rule

The first line is called a dependency and the second line is called a rule. The first character on a rule line must be the TAB character. One or more rule lines may follow a dependency.

Example 1:
    The executable mine depends on the object files mine.o and minelib.o. The following segment describes that dependency relationship.

mine:    mine.o minelib.o
             acc -o mine mine.o minelib.o
    If mine.o or minelib.o has been modified since mine was last changed, normally mine would be updated by executing acc -o mine mine.o minelib.o. However, if the description above is contained in a file called makefile, simply typing the word make would perform the needed updates. (Note: The description lines must start with TAB, so there is an invisible TAB character before the acc.)

Example 2:
    In more complicated situations, a given target can depend on components that are themselves targets. The following makefile description file has three targets. 

my:          my.o mylib.o
                acc -o my my.o mylib.o
my.o:       my.c myinc.h
                acc -c my.c
mylib.c:   mylib.c myinc.h
                acc -c mylib.c
Just type make to do the required updates.

Here is illustration of a session involving the make utility:

$ atrium7:/users4/st/mgorenbu/cis60.1> ls -l
-rw-------   1 mgorenbu cis          121 Feb 18 00:06 Makefile
-rwx--x--x  1 mgorenbu cis         7084 Feb 18 00:07 my
-rw-r--r--   1 mgorenbu cis          411 Feb 18 00:04 my.c
-rw-------   1 mgorenbu cis         1976 Feb 18 00:05 my.o
-rw-r--r--   1 mgorenbu cis          102 Feb 17 23:46 myinc.h
-rw-r--r--   1 mgorenbu cis          269 Feb 18 00:05 mylib.c
-rw-------   1 mgorenbu cis         1276 Feb 18 00:05 mylib.o
$ atrium7:/users4/st/mgorenbu/cis60.1> pico my.c                                    // I change my.c

$ atrium7:/users4/st/mgorenbu/cis60.1> ls -l
-rw-------   1 mgorenbu cis          121 Feb 18 00:06 Makefile
-rwx--x--x  1 mgorenbu cis         7084 Feb 18 00:07 my
-rw-r--r--   1 mgorenbu cis          452 Feb 23 13:44 my.c
-rw-------   1 mgorenbu cis         1976 Feb 18 00:05 my.o
-rw-r--r--   1 mgorenbu cis          102 Feb 17 23:46 myinc.h
-rw-r--r--   1 mgorenbu cis          269 Feb 18 00:05 mylib.c
-rw-------   1 mgorenbu cis         1276 Feb 18 00:05 mylib.o

$ atrium7:/users4/st/mgorenbu/cis60.1> make
acc -c my.c
acc -o my my.o mylib.o

$ atrium7:/users4/st/mgorenbu/cis60.1> ls -l
-rw-------   1 mgorenbu cis          121 Feb 18 00:06 Makefile
-rwx--x--x  1 mgorenbu cis         7128 Feb 23 13:45 my                              // my - new executable
-rw-r--r--   1 mgorenbu cis          452 Feb 23 13:44 my.c                             // my.c was recompiled
-rw-------   1 mgorenbu cis         2056 Feb 23 13:45 my.o                            // my.o was recompiled
-rw-r--r--   1 mgorenbu cis          102 Feb 17 23:46 myinc.h                        // myinc.h was not recompiled
-rw-r--r--   1 mgorenbu cis          269 Feb 18 00:05 mylib.c                         // myinc.c was not recompiled
-rw-------   1 mgorenbu cis         1276 Feb 18 00:05 mylib.o                        // myinc.o was not recompiled


The following materials were used in creating the page:

Michael Gorenburg


Back to CIS 22 home page