A Quick Introduction to Make
A quick introduction to how Make works and some tips for using it.
[Doc 8300 content is unavailable at this time.]
Background
Make is a reduction language.
An Example
Given the Makefile
a: b c cat b c > a b: echoTypinghi - b> b c: echohi - c> c
$ make a
Executes:
echo
creating a file namedhi - b
> bb
containinghi - b
.echo
creating a file namedhi - c
> cc
containinghi - c
cat b c > a
creating a file nameda
containing the contents of b and a (concatenating them, redirecting the output toa
).
Makefile Structure
Generically Makefiles consist of simple rules
of the form:
TARGET ... : PREREQUISITES ... COMMAND ...
More than one target can be built by a rule, and a rule can have any number of prequisites.
Important thing about Makefile Rules
- Make requires real tabs before the COMMAND
Useful Make tricks
-
Using variables in Makefiles. Variables in Makefiles look and generally
function like variables in Bourne Shell, with the exception that
variables must be referenced as
$(VARIABLE)
.For example,
PREFIX=/home/miner $(PREFIX)/a: b c cat b c > $(PREFIX)/a b: echo "hi - b" > b c: echo "hi - c" > c
-
Create aliases for commands. You'll save yourself a lot of headaches if
you create variables pointing to the command to use you will be able to
easier integrate with AutoConf, and be able to change which specific
executable you use if there are broken or outdated versions.
For example,
CAT=/bin/cat ECHO=/bin/echo a: b c $(CAT) b c > a b: $(ECHO) "hi - b" > b c: $(ECHO) "hi - c" > c
When you find that the version of echo you're using doesn'techo
to your satisfaction, you can easily change which one you're using. -
Creating directories with Make. Directories can be created just as
easily as regular files, and can save you a lot of fumbling and
debugging.
For example,
PREFIX=/usr/local/stuff BINDIR=$(PREFIX)/bin MKDIR=mkdir $(BINDIR): $(PREFIX) $(MKDIR) $(BINDIR) $(PREFIX): $(MKDIR) $(PREFIX)
-
Using
install
targets. Targets in a Makefile don't need to be real files. The pseudo targetinstall
is used to gather the files to be installed.For example,
PREFIX=/usr/local/stuff CAT=cat CP=cp MKDIR=mkdir a: b c $(CAT) b c > a b: echo "hi - b" > b c: echo "hi - c" > c $(PREFIX): $(MKDIR) $(PREFIX) $(PREFIX)/a: a $(PREFIX) $(CP) a $(PREFIX)/a install: $(PREFIX)/a echo "Installed!"
So....
With the Makefile illustrated above, typing
$ make install
Causes the following things to happen (assuming that none of the files or directories already exist):
Make
expands all variables in the file:$(PREFIX)
becomes/usr/local/stuff
$(CAT)
becomescat
$(CP)
becomescp
$(MKDIR)
becomesmkdir
Make
looks for a target in theMakefile
namedinstall
, and attempts to follow the rules to create it, but notices that it has a prerequisite of/usr/local/stuff/a
.Make
looks for a target in the Makefile named/usr/local/stuff/a
and attempts to follow the rules to create it, but notices that it has prerequisites ofa
and/usr/local/stuff
.Make
looks for a target nameda
, and attempts to follow the rules to create it, but notices that is has prerequisites ofb
andc
.Make
looks for a target namedb
, and creates it by executing:echo "hi-b" > b
Make
looks for a target namedc
, and creates it by executing:echo "hi-c" > c
- Having satisfied the prerequisites of
a
,Make
createsa
by following it's rules and executes:cat b c > a
Make
looks for a target named/usr/local/stuff
, and creates it by executing:mkdir /usr/local/stuff
- Having satisfied the prerequisites of
/usr/local/stuff/a
,Make
creates/usr/local/stuff/a
by following it's rules and executes:cp a /usr/local/stuff/a
- Having satisfied the prerequisites of
install
,Make
follows the rules forinstall
and executes:echo "Installed!"
- And finally, having no other targets to create,
Make
exits.