Node:A simple makefile, Next:, Previous:Rule Introduction, Up:Writing a makefile

A simple makefile

Here is a straightforward makefile that describes the way an executable file called edit depends on eight object files which, in turn, depend on eight C source files and three header files.

In this example, all the C files include defs.h, but only files that define editing commands include command.h, and only low-level files that change the editor buffer include buffer.h.

edit : main.o kbd.o command.o display.o \
       insert.o search.o files.o utils.o
        cc -o edit main.o kbd.o command.o display.o \
                   insert.o search.o files.o utils.o

main.o : main.c defs.h
        cc -c main.c
kbd.o : kbd.c defs.h command.h
        cc -c kbd.c
command.o : command.c defs.h command.h
        cc -c command.c
display.o : display.c defs.h buffer.h
        cc -c display.c
insert.o : insert.c defs.h buffer.h
        cc -c insert.c
search.o : search.c defs.h buffer.h
        cc -c search.c
files.o : files.c defs.h buffer.h command.h
        cc -c files.c
utils.o : utils.c defs.h
        cc -c utils.c
clean :
        rm edit main.o kbd.o command.o display.o \
           insert.o search.o files.o utils.o

We split each long line into two lines using a backslash; this is like using one long line, but easier to read.

To use this makefile to create the executable file called edit, type:


To use this makefile to delete the executable file and all the object files from the directory, type:

make clean

In the example makefile, the targets include the executable file edit, and the object files main.o and kbd.o. The prerequisites are files such as main.c and defs.h. In fact, each .o file is both a target and a prerequisite. Commands include cc -c main.c and cc -c kbd.c.

When a target is a file, it needs to be recompiled or relinked if any of its prerequisites change. In addition, any prerequisites that are themselves automatically generated should be updated first. In this example, edit depends on each of the eight object files; the object file main.o depends on the source file main.c and on the header file defs.h.

A shell command follows each line that contains a target and prerequisites. These shell commands tell make how to update the target file. A tab character must come at the beginning of every command line to distinguish command lines from other lines in the makefile. (Bear in mind that make does not know anything about how the commands work. It is up to you to supply commands that will update the target file properly.)

The target clean is not a file, but merely the name of an action. Since this action is not carried out as part of the other targets, clean is not a prerequisite of any other rule. Consequently, make never does anything with it unless you explicitly type make clean. Not only is this rule not a prerequisite, it does not have any prerequisites itself, so the only purpose of the rule is to run the specified commands. Targets like clean that do not refer to files but are just actions are called phony targets.