Introducing UNIX and Linux |
Advanced shell programmingOverview |
MakefilesWhen developing software you will frequently create files that
depend on the existence and/or state of other files. Consider the
situation where you have a C program, and which you wish to call
In order to compile this C program using the command
The first two lines, which translate the two source files to object code, can be performed in either order. The final command, which links two object code files together, must wait until the first two have been completed. If you were to type in these commands each time, there would be
a danger of making an error and accidentally forgetting to
recompile one of the source files after editing it. Alternatively,
you could place all three commands into a file and then execute
that file as a shell script. This would have the disadvantage that
if you edited (say) In this small example, this might seem a minor problem, but when performing serious system development it is not unusual to have large volumes of code that take a long time to compile. In such a case, it is sensible to minimise the amount of work that has to be done when small changes to the code are made. A tool that makes use of file dependencies is known as
The above example could have as the contents of its makefile: myprogram: prog1.o prog2.o cc -o myprogram prog1.o prog2.o prog1.o: prog1.c cc -c prog1.c prog2.o: prog2.c cc -c prog2.c You will notice two types of line in this file. There are lines
that are not indented - they take the form of a word (known as the
target) followed by a colon, and then followed by
some other words, where the words would usually be names of files.
These lines indicate that the word (file) on the left of the colon
is dependent on the filenames on the right of the semicolon. Thus
The indented lines (which must be indented with a single
TAB, not with Spaces) indicate the action to be
taken if the dependency (which would be shown on the previous line)
is not up-to-date. So, if In order to use makefiles, data in the format discussed above
should be stored in a file with name either
If you invoke
If, while running This utility has many other features, and can handle much more
complex dependencies than the simple ones indicated here. If you
are creating programs as part of a course in Pascal or C (say),
then a simple makefile such as this one will be adequate. Only when
you move on to more complex programming tasks will you need to
examine Two other standard commands, which are principally used within
makefiles, are worth mentioning briefly. Command |
Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck