- The basic idea behind make is simple.You tell make what targets you want to build and then give rules explaining how to build them. You also specify dependencies that indicate when a particular target should be rebuilt.
- make minimizes rebuild times because it is smart enough to determine which files have changed, and thus only rebuilds files whose components have changed.
- make maintains a database of dependency information for your projects and so can verify that all of the files necessary for building a program are available each time you start a build.
- A makefile is a text file database containing rules that tell make what to build and how to build it. A rule consists of the following:
- A target, the "thing" make ultimately tries to create
- A list of one or more dependencies, usually files, required to build the target
- A list of commands to execute in order to create the target from the specified dependencies
- When invoked, GNU make looks for a file named GNUmakefile, makefile, or Makefile, in that order. For some reason, most Linux programmers use the last form, Makefile. Makefile rules have the general form
target : dependency dependency [...]
command
command
[...]
- target is generally the file, such as a binary or object file, that you want created.
- dependency is a list of one or more files required as input in order to create target.
- The commands are the steps, such as compiler invocations, necessary to create target.
- Unless specified otherwise, make does all of its work in the current working directory.
- In addition to the obvious targets, there should always be a clean target. This target removes all the generated object files and programs so that you can start fresh. The rule for this target uses the rm command to remove the files.
Here is what Makefile contains:
reciprocal: main.o reciprocal.o
g++ $(CFLAGS) -o reciprocal main.o reciprocal.o
main.o: main.c reciprocal.hpp
gcc $(CFLAGS) -c main.c
reciprocal.o: reciprocal.cpp reciprocal.hpp
g++ $(CFLAGS) -c reciprocal.cpp
clean:
rm -f *.o reciprocal
This makefile has four rules.
- The first target, reciprocal, is called the default target, this is the file that make tries to create. reciprocal has two dependencies, main.o and reciprocal.o; these two files must exist in order to build editor.
- Following line is the command that make will execute to create editor.This command builds an executable named reciprocal from the two object files.
- The next two rules tell make how to build the individual object files.
- The line with the rule on it must start with a Tab character, or make will get confused.
- The $(CFLAGS) is a make variable.You can define this variable either in the Makefile itself or on the command line. GNU make will substitute the value of the variable when it executes the rule. So, for example, to recompile with optimization enabled, you would do this:
% make clean
rm -f *.o reciprocal
% make CFLAGS=-O2
- How does make know when to rebuild a file?
- If a specified target does not exist in a place where make can find it, make (re)builds it.
- If the target does exist, make compares the timestamp on the target to the timestamp of the dependencies.
- If one or more of the dependencies is newer than the target, make rebuilds the target, assuming that the newer dependency implies some code change that must be incorporated into the target.