How does make work?
Last revision August 4, 2004
Table of contents: |
For each programming project, you normally create a separate directory to hold the source code, include files, object code, etc. In that directory you create a special file called a makefile, which is usually saved under that exact filename makefile, although other names can be used, such as makefile1, makefile2, etc., for alternate versions. The makefile is a type of script that contains instructions for compiling the program or other tasks (such as installing it). It is read by the make program, which then interprets the instructions and performs all steps needed to create the final program.
A makefile contains the following types of information:
- Names of target files that you are trying to maintain, each followed by the list of component files that the target file depends upon. These component files are also specified as targets in turn if they depend upon still other files.
- Executable commands that describe the processing each file needs.
- Macro definitions (like variables) that provide a short-hand way to refer to a list of files or a processing rule. Use of macros makes complicated makefiles easier to maintain since you can change a default file list or processing rule wherever it appears in the makefile just by changing the macro definition. They can be over-ridden by command line options when make is run.
The makefile does not always have to spell everything out in detail, for the make command has certain default rules. For example, it knows that an object code file name.o depends upon the corresponding source code file name.c or name.f and is created from the source code by running the appropriate language compiler.
When executed, make reads the makefile and sets up a table of file dependencies and processing rules for the desired target file. It then examines the dates on each file to see which have been changed since the target file was last made. If a file has been changed, the other files dependent on it are re-made, starting at the lowest level and proceeding up until the final target file is correctly remade.
make normally prints each processing command on the terminal before executing it, so you can see what is happening.
make also normally stops execution if any processing command returns a non-zero (error) status.