1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Monday, April 20, 2009

The Makefile

The Makefile

The previous section described dependencies between files. This section describes the make program in more detail by describing the file it uses, called makefile or Makefile. This file determines the relationships between the source, object and executable files.

Translating the dependency graph

[Image unavailable]

Each dependency shown in the graph is circled with a corresponding color in the Makefile, and each uses the following format:

target : source file(s)
command (must be preceded by a tab)
A target given in the Makefile is a file which will be created or updated when any of its source files are modified. The command(s) given in the subsequent line(s) (which must be preceded by a tab character) are executed in order to create the target file.

Listing dependencies

project1: data.o main.o io.o
cc data.o main.o io.o -o project1
data.o: data.c data.h
cc -c data.c
main.o: data.h io.h main.c
cc -c main.c
io.o: io.h io.c

Using the Makefile with make

Once you have created your Makefile and your corresponding source files, you are ready to use make. If you have named your Makefile either Makefile or makefile, make will recognize it. If you do not wish to call your Makefile one of these names, you can use make -f mymakefile. The order in which dependencies are listed is important. If you simply type make and then return, make will attempt to create or update the first dependency listed.

You can also specify one of the other targets listed in the Makefile, and only that target (and its corresponding source files) would be made. For example, if we typed make, the output of make would look as follows:

% make cc -c data.c cc -c main.c cc -c io.c cc data.o main.o io.o -o project1 % When making its targets, make first checks the source files and attempts to create or update the source files. That is why data.o, main.o and io.o were created before attempting to create the target: project1.

cc -c io.c

Note that in the Makefile shown above, the .h files are listed, but there are no references in their corresponding commands. This is because the .h files are referred within the corresponding .c files through the #include "file.h". If you do not explicitly include these in your Makefile, your program will not be updated if you make a change to your header (.h) files.

Note: Comments can be placed in a Makefile by placing a pound s ign (#) in front of it.



 
# #