In a single rule you specify a dependence (between files) and how to solve that dependence.
Format:
target: pre-1 pre-2 ... pre-n
TAB command
Example:
mytest.o: mytest.c
$(CC) -c mytest.c
Explanation: If mytest.c is younger than mytest.o, mytest.c must be compiled to generate a new version of mytest.o.
Implicit Rules
Implicit rules are predefined rules for certain file extensions. E.g. there
is a predefined rule how to compile a .c file to generate a .o file (with the same name prefix).
condition
rule
t.o: t.c
$(CC) -c $(CPPFLAGS) $(CFLAGS)
t.o: t.cc
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)
t.o: t.f
$(FC) -c $(FFLAGS)
t.o: t.F
$(FC) -c $(FFLAGS) $(CPPFLAGS)
t.o: t.s
$(AS) $(ASFLAGS
t: t.o
$(CC) $(LDFLAGS) t.o $(LOADLIBES) $(LDLIBS)
t.c: t.y
$(YACC) $(YFLAGS)
t.c: t.l
$(LEX) $(LFLAGS)
t.ln: t.c
$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i
t.dvi: t.tex
$(TEX)
With this implicit rules it is possible to specify a dependence, but leave
the generation part to the implicit rule.
Example:
mytest.o: mytest.c
You don't have to specify how a .o file can be generated from a .c file
as this is already specified in an implicit rule.
Logical Names
name
meaning
default (on Linux)
AR
archive-maintaining program
ar
AS
assembler
as
ASFLAGS
assembler flags
CC
C compiler
cc
CFLAGS
C compiler flags
CPP
C preprocessor
$(CC) -E
CPPFLAGS
C preprocessor flags
CXX
C++ compiler
c++
CXXFLAGS
C++ compiler flags
FC
Fortran77 compiler
f77
FFLAGS
Fortran77 compiler flags
LD
linker
ld
LDFLAGS
linker flags
LDLIBS
load libraries
LEX
lex scanner generator
flex
LFLAGS
lex flags
LINT
lint static C program checker
lint
LINTFLAGS
lint flags (lint)
LOADLIBS
load libraries
MAKE
make command
make
RM
remove file
rm -f
TEX
TeX document system
latex
YACC
yacc compiler generator
bison
YFLAGS
yacc flags
Special Targets
name
meaning
usage example
.PHONY
target that is not a name of a file
.PHONY: clean
.SUFFIXES
list of names for suffix rules
.SUFFIXES .c .o
.DEFAULT
command executed if no other rules applies
.DEFAULT cat
.PRECIOUS
these targets are not deleted if make is interrupted
.INTERMEDIATE
files listed are treated as intermediate files
.INTERMEDIATE .o
.SECONDARY
files listed are treated as intermediate files
.SECONDARY .o
.IGNORE
.SILENT
command executed are not printed out
.SILENT .o
Variables
meaning
usage example
setting a variable (evaluated each time variable is used)
CPP = $(CC) -E
setting a variable (evaluated once at definition point)
CPP := $(CC) -E
value of a variable
$(CC)
Automatic Variables
name
meaning
$@
target
$%
target member if target is an archive
$<
pre-1
$?
all pre-x that are newer than target
$^
all pre-x (without duplicates)
$+
all pre-x (with duplicates preserved)
$*
stem with which rule matches
Example 1
Assume you have a file mytest.c that should be compiled and linked
to mytest.exe with certein compile and link flags.
We use here in a first approach explicit rules.
Assume you have a file mytest.c that should be compiled and linked
to mytest.exe . Instead of explicitly defining special rules
you can define generales rule how to compile any .c file to a .o file,
and a general rule how to link a .o file to a .exe file.
# example of a make file (Makefile)
# define include search path
INCLUDE_DIR = -I/usr/local/include
# C compiler flags
CFLAGS = $(INCLUDE_DIR) -O
# additional Libraries
LDLIBS = -L/usr/local/lib32 -lFHBRS
# phony targets
.PHONY: clean default
# clear suffix list and define new one for .c and .o files
.SUFFIXES:
.SUFFIXES: .c .o .exe
# define general rule to compile .c to .o
%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
# define general rule to link .exe from .o
%.exe: %.o
$(CC) -o $@ $< $(LDLIBS)
# first target
# (implicit rules used: mytest.c -> mytest.o -> mytest.exe)
mytest.exe:
# make clean
clean::
-rm -f mytest.exe *.o