Writing a makefile
make program automatically determines which pieces of
a large program need to be recompiled, and issues the commands to
compile them. You need a file called a makefile to tell
make what to do. Most often, the makefile tells
make how to compile and link a program.
In this section, we will discuss a simple makefile that describes how to
compile and link a text editor which consists of eight C source files
and three header files. The makefile can also tell
make how to
run miscellaneous commands when explicitly asked (for example, to remove
certain files as a clean-up operation).
Although the examples in this section show C programs, you can use
make with any programming language whose compiler can be run
with a shell command. Indeed,
make is not limited to programs.
You can use it to describe any task where some files must be updated
automatically from others whenever the others change.
Your makefile describes the relationships among files in your program and provides commands for updating each file. In a program, typically, the executable file is updated from object files, which are in turn made by compiling source files.
Once a suitable makefile exists, each time you change some source files,
this simple shell command:
suffices to perform all necessary recompilations. The
program uses the makefile database and the last-modification times of
the files to decide which of the files need to be updated. For each of
those files, it issues the commands recorded in the database.
You can provide command line arguments to
make to control which
files should be recompiled, or how.
make recompiles the editor, each changed C source file
must be recompiled. If a header file has changed, each C source file
that includes the header file must be recompiled to be safe. Each
compilation produces an object file corresponding to the source file.
Finally, if any source file has been recompiled, all the object files,
whether newly made or saved from previous compilations, must be linked
together to produce the new executable editor.