Anda di halaman 1dari 78

Makefile

Makefile

1 78

2005 10 14

Makefile

...............................................................................................................................6
...............................................................................................6
Makefile ............................................................................................................7
Makefile ..............................................................................................................7
...........................................................................................................................8
make........................................................................................................9
makefile....................................................................................................10
make..........................................................................................................11
makefile....................................................................................................12
.....................................................................................................13
Makefile ..........................................................................................................13
Makefile ....................................................................................................13
1 ..............................................................................................................14
2 ..............................................................................................................14
3 ..........................................................................................................14
4 ..............................................................................................................14
5 ......................................................................................................................14
Makefile ........................................................................................................15
Makefile ....................................................................................................15
MAKEFILES................................................................................................16
make..........................................................................................................16
.....................................................................................................................17
.........................................................................................................................17
.....................................................................................................................17
.....................................................................................................18
.........................................................................................................................19
.............................................................................................................................20
.............................................................................................................................22
.........................................................................................................................22
.............................................................................................................24
.......................................................................................................................25
.........................................................................................................................26
.........................................................................................................................26
.........................................................................................................................27
make..............................................................................................................28
.....................................................................................................................30
.......................................................................................................................30
.....................................................................................................................31
.................................................................................................................32
.................................................................................................................34
.....................................................................................................................37
override ..........................................................................................................37
.........................................................................................................................38
2 78

2005 10 14

Makefile

.........................................................................................................................38
.........................................................................................................................39
.........................................................................................................................40
...............................................................................................................40
.................................................................................................................................40
.................................................................................................................................42
.......................................................................................................................43
.............................................................................................................44
.............................................................................................................44
1subst .......................................................................................................................44
2patsubst.................................................................................................................45
3strip .........................................................................................................................45
4findstring ...............................................................................................................46
5filter.........................................................................................................................46
6filter-out.................................................................................................................46
7sort ..........................................................................................................................47
8word ........................................................................................................................47
9wordlist ..................................................................................................................47
10words....................................................................................................................47
11firstword..............................................................................................................48
12....................................................................................................48
.............................................................................................................48
1dir.............................................................................................................................48
2notdir ......................................................................................................................48
3suffix .......................................................................................................................49
4basename..............................................................................................................49
5addsuffix................................................................................................................49
6addprefix ...............................................................................................................49
7join...........................................................................................................................50
foreach ................................................................................................................50
if ............................................................................................................................50
call ..........................................................................................................................51
origin .....................................................................................................................51
undefined .................................................................................................................52
default .......................................................................................................................52
file ...............................................................................................................................52
command line.........................................................................................................52
override ....................................................................................................................52
automatic .................................................................................................................52
shell .......................................................................................................................53
make..........................................................................................................53
1error ........................................................................................................................53
2warning ..................................................................................................................54
make ..............................................................................................................54
3 78

2005 10 14

Makefile

make..............................................................................................................54
Makefile ................................................................................................................54
.........................................................................................................................55
all ................................................................................................................................56
clean ..........................................................................................................................56
install .........................................................................................................................56
print ...........................................................................................................................56
tar ...............................................................................................................................56
dist..............................................................................................................................56
TAGS ..........................................................................................................................56
checktest ..........................................................................................................56
.........................................................................................................................57
make..................................................................................................................57
...................................................................................................................61
.................................................................................................................61
.................................................................................................................62
1C ...........................................................................................63
2C++ .....................................................................................63
3Pascal..................................................................................63
4Fortran/Ratfor..................................................................63
5Fortran/Ratfor..............................................................63
6Modula-2 ...........................................................................63
7 ..............................................................................64
8Object .................................................................................64
9Yacc C .....................................................................................64
10Lex C.....................................................................................64
11Lex Ratfor ...........................................................................65
12CYaccLexLint ..................................65
.....................................................................................................65
1 ..................................................................................................65
2 ..............................................................................................66
.....................................................................................................................67
.................................................................................................................68
1 ..........................................................................................................68
2 ..........................................................................................................69
3 ..............................................................................................................70
4 ..............................................................................................................72
5 ..................................................................................................72
"" .................................................................................................73
.........................................................................................................74
make....................................................................................75
.........................................................................................................75
.................................................................................................75
.................................................................................................76
4 78

2005 10 14

Makefile

.........................................................................................................................76
...........................................................................................................................77

5 78

2005 10 14

Makefile

makefile Winodws Windows


IDE professional makefile
HTML
HTML Unix makefile
makefile
makefile
makefile

makefile Shell makefile


make
make makefile
IDE Delphi makeVisual C++ nmake
Linux GNU makemakefile
makefile
make
GNU make RedHat Linux 8.0make 3.80
make IEEE 1003.2-1992
POSIX.2
C/C++ C/C++

UNIX GCC CC

CC++ pas
Windows .obj UNIX .o
Object Filecompile Object File
link

C/C++

O OBJ
O OBJ

Object File
6 78

2005 10 14

Makefile

Windows Library File) .lib UNIX


Archive File .a

Object File Object File


Linker Error
VC
Link 2001
Object File.
GNU make

Makefile
make Makefile make

Makefile
GNU make 8 C 3
Makefile make
1 C
2 C C

3 C

Makefile make
make

Makefile
Makefile Makefile
target ... : prerequisites ...
command
7 78

2005 10 14

Makefile

...
...
target Object File
Label
prerequisites target
command make
Shell
target
prerequisites command prerequisites
target command
Makefile Makefile
Makefile
Makefile Makefile

3 8 C
Makefile
edit : main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
cc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
8 78

2005 10 14

Makefile

utils.o : utils.c defs.h


cc -c utils.c
clean :
rm edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
\ Makefile
Makefilemakefilemake
edit
make clean
makefile target edit *.o

prerequisites .c .h .o
.o edit

Tab make
make targets prerequisites prerequisites
targets target make

clean C
lable make
make
lable makefile

make
make
1make Makefilemakefile
2target

edit
3 edit
edit .o edit
edit
4 edit .o make .o
.o
5 C H make .o .o
make edit
make make
9 78

2005 10 14

Makefile

make make
make

clean
make
make clean

file.c file.o
file.o file.o
edit edit edit
command.hkdb.ocommand.o files.o
edit

makefile
edit
edit : main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
cc -o edit main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
[.o][.o]
clean

makefile makefile
makefile
makefile makefile C

objects, OBJECTS, objs, OBJS, obj, OBJ


obj makefile
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
makefile $(objects)
makefile

10 78

2005 10 14

Makefile

objects = main.o kbd.o command.o display.o \


insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
display.o : display.c defs.h buffer.h
cc -c display.c
insert.o : insert.c defs.h buffer.h
cc -c insert.c
search.o : search.c defs.h buffer.h
cc -c search.c
files.o : files.c defs.h buffer.h command.h
cc -c files.c
utils.o : utils.c defs.h
cc -c utils.c
clean :
rm edit $(objects)

.o objects

make
GNU make
[.o] make

make [.o][.c] make


whatever.o whatever.c whatever.o cc -c
whatever.c makefile
makefile

11 78

2005 10 14

Makefile

objects = main.o kbd.o command.o display.o \


insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
main.o : defs.h
kbd.o : defs.h command.h
command.o : defs.h command.h
display.o : defs.h buffer.h
insert.o : defs.h buffer.h
search.o : defs.h buffer.h
files.o : defs.h buffer.h command.h
utils.o : defs.h
.PHONY : clean
clean :
rm edit $(objects)
make .PHONYclean

makefile
make [.o][.h]
[.h] make
makefile
objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
$(objects) : defs.h
kbd.o command.o files.o : command.h
display.o insert.o search.o files.o : buffer.h
.PHONY : clean
clean :
rm edit $(objects)
12 78

2005 10 14

Makefile

makefile

.o

Makefile .o

clean:
rm edit $(objects)

.PHONY : clean
clean :
-rm edit $(objects)
.PHONY clean
rm
clean
make
clean

makefile makefile makefile

Makefile

Makefile

13 78

2005 10 14

Makefile

Makefile

1
Makefile

2
make
Makefile make

3
Makefile C
Makefile

4
Makefile Makefile C
include Makefile C
#if

5
Makefile UNIX Shell #
C/C++// Makefile #
\#
Makefile [Tab]

14 78

2005 10 14

Makefile

Makefile
make GNUmakefile
makefileMakefile
Makefile
GNUmakefile GNU make make
makefile make makefile
Makefile
Makefile Make.Linux
Make.SolarisMake.AIX Makefile make
-f--filemake -f Make.Linux make --file Make.AIX

Makefile
Makefile include Makefile C
#includeinclude
include <filename>
filename Shell include
[Tab]include <filename>
Makefilea.mkb.mkc.mk
foo.make$(bar) e.mk f.mk
include foo.make *.mk $(bar)

include foo.make a.mk b.mk c.mk e.mk f.mk


make include Makefile
C/C++#include
make make

1 make -I--include-dir make

2<prefix>/include/usr/local/bin /usr/include
make make
makefile make
make make
include -
15 78

2005 10 14

Makefile

-include <filename>
include make
sinclude

MAKEFILES
MAKEFILESmake
include Makefile include
Makefile
make

make Makefile
Makefile

make
GNU make
make
1 Makefile
2 include Makefile
3
4
5
6
7
1-5 6-7
make make make

make

16 78

2005 10 14

Makefile

Makefile Makefile
make
Makefile
make

foo.o : foo.c defs.h # foo


cc -c -g foo.c
foo.o foo.c
defs.h cc -c -g foo.c Tab

1foo.o foo.c defs.h foo.c defs.h


foo.o foo.o
2foo.o cc foo.o
foo.c include defs.h

targets : prerequisites
command
...

targets : prerequisites ; command


command
...

17 78

2005 10 14

Makefile

targets

command target:prerequisites[Tab
] prerequisites
prerequisites

\make
make
make UNIX Shell/bin/sh

make
*?[...] Unix B-Shell ~
~/test$HOME
test ~hchen/test hchen test

Unix make Windows MS-DOS


HOME
*.c c
*\\*
*

clean:
rm -f *.o
Shell

print: *.c
lpr -p $?
touch print
print [.c]
$?
objects = *.o
[*.o]objects
*.oMakefile C/C++
objects [.o]

18 78

2005 10 14

Makefile

objects := $(wildcard *.o)


wildcard Makefile

make
make make
Makefile VPATH
make make

VPATH = src:../headers
src../headersmake

make vpath
make VPATH

1vpath <pattern> <directories>


<pattern><directories>
2vpath <pattern>
<pattern>
3vpath

vapth <pattern>%%
%.h.h<pattern>
<directories><pattern>
vpath %.h ../headers
make ../headers.h

19 78

2005 10 14

Makefile

vpath vpath
<pattern><pattern>make vpath

vpath %.c foo


vpath % blish
vpath %.c bar
.cfooblishbar
vpath %.c foo:bar
vpath % blish
.cfoobar
blish

clean
clean:
rm *.o temp
clean
make clean

clean
make

.PHONY
make

.PHONY : clean
cleancleanmake
clean

20 78

2005 10 14

Makefile

.PHONY: clean
clean:
rm *.o temp

Makefile
make
Makefile
all : prog1 prog2 prog3
.PHONY : all
prog1 : prog1.o utils.o
cc -o prog1 prog1.o utils.o
prog2 : prog2.o
cc -o prog2 prog2.o
prog3 : prog3.o sort.o utils.o
cc -o prog3 prog3.o sort.o utils.o
Makefile all

all
.PHONY : allall

.PHONY: cleanall cleanobj cleandiff


cleanall : cleanobj cleandiff
rm program
cleanobj :
rm *.o
cleandiff :
rm *.diff
make cleancleanobjcleandiff
make cleanallmake cleanobj
make cleandiff
21 78

2005 10 14

Makefile

Makefile

$@

bigoutput littleoutput : text.g


generate text.g -$(subst output,,$@) > $@

bigoutput : text.g
generate text.g -big > bigoutput
littleoutput : text.g
generate text.g -little > littleoutput
-$(subst output,,$@)$ Makefile subst
$@
$@

<targets ...>: <target-pattern>: <prereq-patterns ...>


<commands>
....
targets
target-parrtern targets
prereq-parrterns target-parrtern

22 78

2005 10 14

Makefile

<target-parrtern>%.o<target>.o
<prereq-parrterns>%.c<target-parrtern>
<target-parrtern>%[.o]
[.c]
%
%\%

objects = foo.o bar.o


all: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@

$object %.o.o
foo.o bar.o$object %.c
%.o%foo bar.c
foo.c bar.c$<$@$<
foo.c bar.c$@foo.o bar.o

foo.o
$(CC)
bar.o
$(CC)

: foo.c
-c $(CFLAGS) foo.c -o foo.o
: bar.c
-c $(CFLAGS) bar.c -o bar.o

%.o

files = foo.elc bar.o lose.o


$(filter
$(CC) -c
$(filter
emacs -f

%.o,$(files)): %.o: %.c


$(CFLAGS) $< -o $@
%.elc,$(files)): %.elc: %.el
batch-byte-compile $<

$(filter %.o,$(files)) Makefile filter $filter


%.o Makefile

23 78

2005 10 14

Makefile

Makefile main.c
#include "defs.h"
main.o : main.c defs.h
C
Makefile
C/C++
C/C++-M

cc -M main.c

main.o : main.c defs.h

GNU C/C++
-MM-M
gcc -M main.c
main.o: main.c defs.h /usr/include/stdio.h /usr/include/features.h \
/usr/include/sys/cdefs.h /usr/include/gnu/stubs.h \
/usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stddef.h \
/usr/include/bits/types.h /usr/include/bits/pthreadtypes.h \
/usr/include/bits/sched.h /usr/include/libio.h \
/usr/include/_G_config.h /usr/include/wchar.h \
/usr/include/bits/wchar.h /usr/include/gconv.h \
/usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stdarg.h \
/usr/include/bits/stdio_lim.h

gcc -MM main.c


main.o: main.c defs.h
Makefile
Makefile Makefile
GNU
24 78

2005 10 14

Makefile

name.c
name.d Makefile [.d][.c]
[.c][.d] make [.d]
Makefile
[.d]
%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$

[.d][.c]rm -f $@
[.d]$<[.c]
$@%.d C name.c%
name$$$$name.d.12345
sed sed

[.d]

main.o : main.c defs.h

main.o main.d : main.c defs.h


[.d][.d]
[.d]

Makefile Makefile include Makefile

sources = foo.c bar.c


include $(sources:.c=.d)
$(sources:.c=.d).c=.d
$(sources)[.c][.d]
include [.d]


25 78

2005 10 14

Makefile

Shell make
[Tab]
Tab
make
UNIX Shell make
/bin/shUNIX Shell Shell
Makefile # C/C++//

make @
make

@echo XXX ......


make XXX ......
@make
echo XXX ......
XXX ......
make make -n--just-print
Makefile

make -s--slient

make

cd cd

exec:
cd /home/hchen
pwd

26 78

2005 10 14

Makefile

exec:
cd /home/hchen; pwd
make exec cd pwd
Makefile cd pwd /home/hchenmake
SHELL Shell UNIX
Shell/bin/sh MS-DOS MS-DOS SHELL
UNIX make SHELL

PATH MS-DOS
.exe.com.bat.sh

make make

make

mkdir
mkdir
mkdir mkdir

Makefile -
Tab
clean:
-rm -f *.o
make -i--ignore-errors
Makefile .IGNORE

make -k--keep-going

27 78

2005 10 14

Makefile

make

Makefile Makefile
Makefile
Makefile
subdir Makefile
Makefile
subsystem:
cd subdir && $(MAKE)

subsystem:
$(MAKE) -C subdir
$(MAKE) make
subdir make
Makefile Makefile Makefile
Makefile
Makefile
-e
Makefile
export <variable ...>
Makefile
unexport <variable ...>

export variable = value

variable = value
export variable

28 78

2005 10 14

Makefile

export variable := value

variable := value
export variable

export variable += value

variable += value
export variable
export

SHELL MAKEFLAGS
export Makefile MAKEFILES make
Makefile make Makefile
MAKEFILES Makefile

make -C,-f,-h-o
-W Makefile

subsystem:
cd subdir && $(MAKE) MAKEFLAGS=
MAKEFLAGS
-t,-n,-q

-w--print-directory
make make
/home/hchen/gnu/makemake -w

29 78

2005 10 14

Makefile

make: Entering directory `/home/hchen/gnu/make'.


make
make: Leaving directory `/home/hchen/gnu/make'
-C make Makefile -w
-s--slient--no-print-directory-w

Makefile
defineendef
define run-yacc
yacc $(firstword $^)
mv y.tab.c $@
endef
run-yacc Makefile
defineendef Yacc
Yacc y.tab.c

foo.c : foo.y
$(run-yacc)

run-yacc$^foo.y$@foo.c$
make


Makefile C/C++
Makefile C/C++
Makefile Makefile
Makefile
:
30 78

2005 10 14

Makefile

#=fooFooFOO
Makefile
MakeFlags
$<$@

$
{}$
$$

objects = program.o foo.o utils.o


program : $(objects)
cc -o program $(objects)
$(objects) : defs.h
C/C++
foo = c
prog.o : prog.$(foo)
$(foo)$(foo) -$(foo) prog.$(foo)

prog.o : prog.c
cc -c prog.c
Makefile Makefile

31 78

2005 10 14

Makefile

Makefile

==

foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:
echo $(foo)
make all$(foo)Huh? $(foo)$(bar)
$(bar)$(ugh)$(ugh)Huh?

CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
CFLAGS-Ifoo -Ibar -O

CFLAGS = $(CFLAGS) -O

A = $(B)
B = $(A)
make make
make
make wildcardshell

make
:=

32 78

2005 10 14

Makefile

x := foo
y := $(x) bar
x := later

y := foo bar
x := later

y := $(x) bar
x := foo
y barfoo bar
make
MAKELEVEL
ifeq (0,${MAKELEVEL})
cur-dir := $(shell pwd)
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif
MAKELEVEL
make make
Makefile

nullstring :=
space := $(nullstring) # end of the line
nullstring Empty space

Empty
#
##
33 78

2005 10 14

Makefile

dir := /foo/bar # directory to put the frobs in


dir /foo/bar 4
$(dir)/file
?=
FOO ?= bar
FOO FOO bar FOO

ifeq ($(origin FOO), undefined)


FOO = bar
endif

$(var:a=b)${var:a=b}var
aab

foo := a.o b.o c.o


bar := $(foo:.o=.c)
$(foo)$(foo)
.o.c$(bar)a.c b.c
c.c

foo := a.o b.o c.o


bar := $(foo:%.o=%.c)
%
$(bar)a.c b.c c.c

34 78

2005 10 14

Makefile

x = y
y = z
a := $($(x))
$(x)y$($(x))$(y)$(a)z

x=yx=$(y)

x
y
z
a

= y
= z
= u
:= $($($(x)))

$(a)u

x
y
z
a

= $(y)
= z
= Hello
:= $($(x))

$($(x))$($(y))$(y)za:=$(z)
Hello

x = variable1
variable2 := Hello
y = $(subst 1,2,$(x))
z = y
a := $($($(z)))
$($($(z))) $($(y)) $($(subst
1,2,$(x)))$(x)variable1subst variable11
2variable1variable2
$(a)$(variable2)Hello

first_second = Hello
a = first
35 78

2005 10 14

Makefile

b = second
all = $($a_$b)
$a_$bfirst_second$(all)Hello

a_objects := a.o b.o c.o


1_objects := 1.o 2.o 3.o
sources := $($(a1)_objects:.o=.c)
$(a1)a$(sources)a.c b.c c.c
$(a1)1$(sources)1.c 2.c 3.c

ifdef do_sort
func := sort
else
func := strip
endif
bar := a d b g q c
foo := $($(func) $(bar))
do_sortfoo := $(sort a d b g q c)$(foo)
a b c d g qdo_sortfoo := $(sort a d bg q
c) strip

dir = foo
$(dir)_sources := $(wildcard $(dir)/*.c)
define $(dir)_print
lpr $($(dir)_sources)
endef
dirfoo_sourcesfoo_print

36 78

2005 10 14

Makefile

+=
objects = main.o foo.o bar.o utils.o
objects += another.o
$(objects)main.o foo.o bar.o utils.o another.oanother.o

+=
objects = main.o foo.o bar.o utils.o
objects := $(objects) another.o
+=
+==+=
:=+=:=

variable := value
variable += more

variable := value
variable := $(variable) more

variable = value
variable += more
=+==
make

override
make Makefile
37 78

2005 10 14

Makefile

Makefile override

override <variable> = <value>


override <variable> := <value>

override <variable> += <more text>


define define ovveride

override define foo


bar
endef

define define

define endef
=
[Tab] define [Tab]
make
define
define two-lines
echo foo
echo $(bar)
endef

make make Makefile


38 78

2005 10 14

Makefile

Makefile make
make -e Makefile

CFLAGS
Makefile
Makefile CFLAGS Makefile

make Makefile
Makefile
Makefile
exprot

Makefile

Makefile
$<

Target-specific Variable

<target ...> : <variable-assignment>


<target ...> : overide <variable-assignment>
<variable-assignment>=:=+=
= make

prog : CFLAGS = -g
prog : prog.o foo.o bar.o
$(CC) $(CFLAGS) prog.o foo.o bar.o
prog.o : prog.c
$(CC) $(CFLAGS) prog.c
39 78

2005 10 14

Makefile

foo.o : foo.c
$(CC) $(CFLAGS) foo.c
bar.o : bar.c
$(CC) $(CFLAGS) bar.c
$(CFLAGS) prog
prog.o foo.o bar.o
$(CFLAGS)-g

GNU make Pattern-specific Variable

make %
[.o]
%.o : CFLAGS = -O

<pattern ...> : <variable-assignment>


<pattern ...> : override <variable-assignment>
override make


make

$(CC)gcc GNU

40 78

2005 10 14

Makefile

libs_for_gcc = -lgnu
normal_libs =
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
else
$(CC) -o foo $(objects) $(normal_libs)
endif
foo$(CC)

ifeqelse endififeq

else endif
endif
$(CC)gcc foo
foo: $(objects)
$(CC) -o foo $(objects) $(libs_for_gcc)
$(CC)gcccc foo
foo: $(objects)
$(CC) -o foo $(objects) $(normal_libs)

libs_for_gcc = -lgnu
normal_libs =
ifeq ($(CC),gcc)
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif
foo: $(objects)
$(CC) -o foo $(objects) $(libs)

41 78

2005 10 14

Makefile

<conditional-directive>
<text-if-true>
endif

<conditional-directive>
<text-if-true>
else
<text-if-false>
endif
<conditional-directive>ifeq
ifeq
ifeq
ifeq
ifeq
ifeq
ifeq

(<arg1>,
'<arg1>'
"<arg1>"
"<arg1>"
'<arg1>'

<arg2>)
'<arg2>'
"<arg2>"
'<arg2>'
"<arg2>"

arg1arg2 make

ifeq ($(strip $(foo)),)


<text-if-empty>
endif
strip Empty
<text-if-empty>
ifneq
ifneq
ifneq
ifneq
ifneq
ifneq

(<arg1>,
'<arg1>'
"<arg1>"
"<arg1>"
'<arg1>'

<arg2>)
'<arg2>'
"<arg2>"
'<arg2>'
"<arg2>"

42 78

2005 10 14

Makefile

arg1arg2ifeq
ifdef
ifdef <variable-name>
<variable-name>
<variable-name>ifdef

bar =
foo = $(bar)
ifdef foo
frobozz = yes
else
frobozz = no
endif

foo =
ifdef foo
frobozz = yes
else
frobozz = no
endif
$(frobozz)yesno
ifndef
ifndef <variable-name>
ifdef
<conditional-directive>[Tab]
#elseendif
[Tab]
make Makefile
$@

make


43 78

2005 10 14

Makefile

Makefile
make

$
$(<function> <arguments>)

${<function> <arguments>}
<function>make <arguments>
,$

$(subst a,b,$(x))
$(subst a,b,${x})

comma:= ,
empty:=
space:= $(empty) $(empty)
foo:= a b c
bar:= $(subst $(space),$(comma),$(foo))
$(comma)$(space)$(empty)$(foo)
a b c$(bar)subst

$(foo)$(bar)a,b,c

1subst
$(subst <from>,<to>,<text>)
subst
<text><from><to>

44 78

2005 10 14

Makefile

$(subst ee,EE,feet on the street)


feet on the street ee EE fEEt on the
strEEt

2patsubst
$(patsubst <pattern>,<replacement>,<text>)
patsubst
<text>Tab
<pattern><replacement><pattern>
%<replacement>%<replacement>
%<pattern>%
\\%
%

$(patsubst %.c,%.o,x.c.c bar.c)


x.c.c bar.c[%.c][%.o]x.c.o bar.o

$(var:<pattern>=<replacement>)

$(patsubst <pattern>,<replacement>,$(var))
$(var: <suffix>=<replacement>)
$(patsubst %<suffix>,%<replacement>,$(var))
objects = foo.o bar.o baz.o
$(objects:.o=.c)$(patsubst %.o,%.c,$(objects))

3strip
$(strip <string>)
strip
<string>

45 78

2005 10 14

Makefile

$(strip a b c )
a b c a b c

4findstring
$(findstring <find>,<in>)
findstring
<in><find>
<find>

$(findstring a,a b c)
$(findstring a,b c)
a

5filter
$(filter <pattern...>,<text>)
filter
<pattern><text><pattern>

<pattern>

sources := foo.c bar.c baz.s ugh.h


foo: $(sources)
cc $(filter %.c %.s,$(sources)) -o foo
$(filter %.c %.s,$(sources))foo.c bar.c baz.s

6filter-out
$(filter-out <pattern...>,<text>)
filter-out
<pattern><text><pattern>

<pattern>

objects=main1.o foo.o main2.o bar.o


mains=main1.o main2.o
$(filter-out $(mains),$(objects)) foo.o bar.o

46 78

2005 10 14

Makefile

7sort
$(sort <list>)
sort
<list>

$(sort foo bar lose)bar foo lose


sort <list>

8word
$(word <n>,<text>)
word
<text><n>
<text><n><n><text>

$(word 2, foo bar baz)bar

9wordlist
$(wordlist <s>,<e>,<text>)
wordlist
<text><s><e><s><e>
<text><s><e><s><text>
<e><text><s><text>

$(wordlist 2, 3, foo bar baz)bar baz

10words
$(words <text>)
words
<text>
<text>
$(words, foo bar baz)3
<text>$(word $(words <te
xt>),<text>)

47 78

2005 10 14

Makefile

11firstword
$(firstword <text>)
firstword
<text>
<text>
$(firstword foo bar)foo
word $(word 1,<text>)

12

make VPATH
CFLAGS

override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))


$(VPATH) src:../headers $(patsubst
%,-I%,$(subst :, ,$(VPATH)))-Isrc -I../headers cc gcc

1dir
$(dir <names...>)
dir
<names>/
./
<names>
$(dir src/foo.c hacks)src/ ./

2notdir
$(notdir <names...>)
48 78

2005 10 14

Makefile

notdir
<names> /

<names>
$(notdir src/foo.c hacks)foo.c hacks

3suffix
$(suffix <names...>)
suffix
<names>
<names>
$(suffix src/foo.c src-1.0/bar.c hacks).c .c

4basename
$(basename <names...>)
basename
<names>
<names>
$(basename src/foo.c src-1.0/bar.c hacks)src/foo src-1.0/b
ar hacks

5addsuffix
$(addsuffix <suffix>,<names...>)
addsuffix
<suffix><names>

$(addsuffix .c,foo bar)foo.c bar.c

6addprefix
$(addprefix <prefix>,<names...>)
addprefix
<prefix><names>

$(addprefix src/,foo bar)src/foo src/bar

49 78

2005 10 14

Makefile

7join
$(join <list1>,<list2>)
join
<list2><list1><list1>
<list2><list1><list2>
<list1><list2><list2>

$(join aaa bbb , 111 222 333)aaa111 bbb222 333

foreach
foreach Makefile
foreach Unix Shell/bin/sh for C-Shell
/bin/csh foreach
$(foreach <var>,<list>,<text>)
<list><var>
<text><text><text>
<text>
foreach
<var><list><text><var>
<list>
names := a b c d
files := $(foreach n,$(names),$(n).o)
$(name)n$(n).o
$(n) foreach
$(files)a.o b.o c.o d.o
foreach <var>foreach
<var> foreach

if
if GNU make ifeq
if

$(if <condition>,<then-part>)
50 78

2005 10 14

Makefile

$(if <condition>,<then-part>,<else-part>)
if else if
<condition> if
<then-part><else-part>
if <condition>
<then-part>
<condition><else-part>
<else-part>
<then-part><else-part>

call
call
call

$(call <expression>,<parm1>,<parm2>,<parm3>...)
make <expression>$(1)$(2)$(3)
<parm1><parm2><parm3><expression> call

reverse = $(1) $(2)


foo = $(call reverse,a,b)
foo a b

reverse = $(2) $(1)


foo = $(call reverse,a,b)
foo b a

origin
origin

$(origin <variable>)
<variable><variable>
$Origin origin
:

51 78

2005 10 14

Makefile

undefined
<variable>origin undefined

default
<variable>CC
environment <variable> Makefile -e

file
<variable> Makefile

command line
<variable>

override
<variable> override

automatic
<variable>
Makefile Makefile
Make.def Make.def bletch
bletch
Make.def
Makefile
ifdef bletch
ifeq "$(origin bletch)" "environment"
bletch = barf, gag, etc.
endif
endif
override
override override
52 78

2005 10 14

Makefile

shell
shell Shell
`shell
awksed

contents := $(shell cat foo)


files := $(shell echo *.c)
Shell
Makefile
Makefile shell

make
make make Makefile
make

1error
$(error <text ...>)
<text ...>error

ifdef ERROR_001
$(error error is $(ERROR_001))
endif

ERR = $(error found an error!)


.PHONY: err
err: ; $(ERR)
ERROR_001 error err
error
53 78

2005 10 14

Makefile

2warning
$(warning <text ...>)
error make ma
ke

make
make make
makefile make

make

make
make
0 -
1 - make 1
2 - make -q make 2
Make

Makefile
GNU make Makefile
GNUmakefilemakefileMakefile

make Makefile
make -f--file--makefile
makefile hchen.mk make
make f hchen.mk

54 78

2005 10 14

Makefile

make -f makefile
make

make makefile
make makefile
make
make make clean
makefile -
=
make make

make MAKECMDGOALS

sources = foo.c bar.c


ifneq ( $(MAKECMDGOALS),clean)
include $(sources:.c=.d)
endif
make clean makefile
foo.dbar.d makefile

.PHONY: all
all: prog1 prog2 prog3 prog4
makefile prog1
prog2 prog3 prog4make all
all makemake prog2
prog2
make makefile
makefile Unix
GNU makefile
makefile

55 78

2005 10 14

Makefile

all

clean
make

install

print

tar
tar

dist
tar Z gz

TAGS

checktest
makefile
makefile GNU
GNU UNIX

makefile makefile

56 78

2005 10 14

Makefile

makefile
make
-n
--just-print
--dry-run
--recon

makefile
-t
--touch
make

-q
--question

-W <file>
--what-if=<file>
--assume-new=<file>
--new-file=<file>
Make
-n

-p-v makefile

make
GNU make 3.80 make
make
-b
-m
make
57 78

2005 10 14

Makefile

-B
--always-make

-C <dir>
--directory=<dir>
makefile -Cmake
make C ~hchen/test C prog
make C ~hchen/test/prog
debug[=<options>]
make
<options>
a all
b basic
v verbose b makefile

i implicit
j jobs PID
m makefile make makefile makefile makefile

-d
--debug=a
-e
--environment-overrides
makefile
-f=<file>
--file=<file>
--makefile=<file>
makefile
-h
--help

-i
--ignore-errors

-I <dir>
58 78

2005 10 14

Makefile

--include-dir=<dir>
makefile -I
-j [<jobsnum>]
--jobs[=<jobsnum>]
make
-j-j
MS-DOS

-k
--keep-going

-l <load>
--load-average[=<load]
max-load[=<load>]
make
-n
--just-print
--dry-run
--recon

-o <file>
--old-file=<file>
--assume-old=<file>
<file>
-p
--print-data-base
makefile makefile
makefilemake -qp
makefile make p f
/dev/null makefile
makefile
-q
--question
0
2
-r
--no-builtin-rules
59 78

2005 10 14

Makefile

make
-R
--no-builtin-variabes
make
-s
--silent
--quiet

-S
--no-keep-going
--stop
-kmake MAKEFLAGS
-k
-t
--touch
UNIX touch

-v
--version
make make
-w
--print-directory
makefile make
--no-print-directory
-w
-W <file>
--what-if=<file>
--new-file=<file>
--assume-file=<file>
<file>-n
-n UNIX touch<file>

--warn-undefined-variables
make

60 78

2005 10 14

Makefile


Makefile
C/C++Unix [.o]Windows [.obj]
Makefile

make
Makefile [.c][.o]
make [.o]

CFLAGS

Makefile

Makefile

make make
make
Makefile
foo : foo.o bar.o
cc o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
Makefile foo.o bar.o
make

make
make [.o]
61 78

2005 10 14

Makefile

[.c] C cc c $(CFLAGS) [.c][.o]

foo.o : foo.c
cc c foo.c $(CFLAGS)
bar.o : bar.c
cc c bar.c $(CFLAGS)
make C cc[.o]

[.o] make

make
make

foo.o : foo.p
foo.pPascal
foo.cfoo.c C
foo.o Pascal C make
foo.o C

make
make make
-r--no-builtin-rules
-r

.SUFFIXES
.out,.a, .ln, .o, .c, .cc, .C, .p, .f, .F, .r, .y, .l, .s, .S, .mod, .sym,
.def, .h, .info, .dvi, .tex, .texinfo, .texi, .txinfo, .w, .ch .web, .sh, .elc, .el

62 78

2005 10 14

Makefile

1 C
<n>.o<n>.c$(CC) c
$(CPPFLAGS) $(CFLAGS)

2 C++
<n>.o<n>.cc<n>.C
$(CXX) c $(CPPFLAGS) $(CFLAGS)
.cc C++
.C

3 Pascal
<n>.o<n>.p$(PC) c $
(PFLAGS)

4 Fortran/Ratfor
<n>.o<n>.r<n>.F<n>.f
:
.f $(FC) c $(FFLAGS)
.F $(FC) c $(FFLAGS) $(CPPFLAGS)
.f $(FC) c $(FFLAGS) $(RFLAGS)

5 Fortran/Ratfor
<n>.f<n>.r<n>.F
Ratfor Fortran Fortran
.F $(FC) F $(CPPFLAGS) $(FFLAGS)
.r $(FC) F $(FFLAGS) $(RFLAGS)

6 Modula-2
<n>.sym <n>.def
$(M2C)$(M2FLAGS) $(DEFFLAGS) <n.o>
63 78

2005 10 14

Makefile

<n>.mod $(M2C) $(M2FLAGS) $(MODFLAGS)

7
<n>.o <n>.sas
$(AS) $(ASFLAGS) <n>.s
<n>.S C cpp$(AS) $(ASFLAGS)

8 Object
<n><n>.o C
ld$(CC) $(LDFLAGS) <n>.o $(LOADLIBES) $(LDLIBS)
Object

x : y.o z.o
x.cy.cz.c
cc -c x.c -o x.o
cc -c y.c -o y.o
cc -c z.c -o z.o
cc x.o y.o z.o -o x
rm -f x.o
rm -f y.o
rm -f z.o
x.c x

9Yacc C
<n>.cn.yYacc
$(YACC) $(YFALGS)
Yacc

10Lex C
<n>.cn.lLex $(LEX)
$(LFALGS)Lex

64 78

2005 10 14

Makefile

11Lex Ratfor
<n>.rn.lLex $(LEX)
$(LFALGS)

12 C Yacc Lex Lint

<n>.ln lint n.c$(LINT)


$(LINTFALGS) $(CPPFLAGS) -i<n>.y<n>.l

makefile make

make -R--nobuiltin-variables

C $(CC) c $(CFLAGS)
$(CPPFLAGS)Make cc$(CC)gcc
$(CFLAGS)-ggcc c -g
$(CPPFLAGS)
CC
CFLAGS

1
AR
ar
AS
as

65 78

2005 10 14

Makefile

CC
C cc
CXX
C++g++
CO
RCS co
CPP
C $(CC) E
FC
Fortran Ratfor f77
GET
SCCS get
LEX
Lex C Ratforlex
PC
Pascal pc
YACC
Yacc C yacc
YACCR
Yacc Ratfor yacc r
MAKEINFO
Texinfo .texi Info makeinfo
TEX
TeX TeX DVI tex
TEXI2DVI
Texinfo TeX DVI texi2dvi
WEAVE
Web TeX weave
CWEAVE
C Web TeX cweave
TANGLE
Web Pascal tangle
CTANGLE
C Web Cctangle
RM
rm f

66 78

2005 10 14

Makefile

ARFLAGS
AR rv
ASFLAGS
.s.S
CFLAGS
C
CXXFLAGS
C++
COFLAGS
RCS
CPPFLAGS
C C Fortran

FFLAGS
Fortran
GFLAGS
SCCS get
LDFLAGS

ld
LFLAGS
Lex
PFLAGS
Pascal
RFLAGS
Ratfor Fortran
YFLAGS
Yacc

[.o]
Yacc [.y][.c] C

[.c] C
[.c][.y] Yacc [.c]
C [.c][.o]
[.c]make

makefile
67 78

2005 10 14

Makefile

rm -f
makefile
.INTERMEDIATE
.INTERMEDIATE mid
make
.SECONDARY.SECONDARY : sec
%.o.PRECIOUS

make
Make foo.c
foomake foo.ofoo
cccc o foo foo.c

"%""%"
"%""%"
"%"
make Makefile "%"

1
"%"
"%""%""%.c"".c"
3
"s.%.c""s."".c"
5
"%""%""%"
68 78

2005 10 14

Makefile

"%""%"
%.o : %.c ; <command ......>
[.c][.o]
"a.o b.o""%c""a.c b.c"
"%"make
make
make make

2
,[.c][.o].
%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
"$@""$<"
""

%.tab.c %.tab.h: %.y


bison -d $<
make [.y] "bison -d <n>.y"
"<n>.tab.c""<n>.tab.h"
"<n>"
"foo" "parse.tab.o" "scan.o" "scan.o"
"parse.tab.h""parse.y""bison -d parse.y"
"parse.tab.o""scan.o"
"parse.tab.o"
"parse.tab.c""scan.o""scan.c""foo""parse.tab.o""scan.o"
foo [.o]

69 78

2005 10 14

Makefile

$@
"$@"

$%
"foo.a
(bar.o)""$%""bar.o""$@""foo.a"Unix
[.a]Windows [.lib]

$<
"%""$<"

$?

$^

$+
"$^"
$*
"%""dir/a.foo.b"
"a.%.b""$*""dir/a.foo"
"$*"
make "$*""foo.c"
".c" make "$*""foo" GNU make

70 78

2005 10 14

Makefile

make"$*"
make "$*"
"$?"
"lib" object object
Makefile
lib : foo.o bar.o lose.o win.o
ar r lib $?
$@$<$%$*

"D""F" GNU make


"dir""notdir""D" Directory
"F" File
"D""F"
$(@D)
"$@""$@""dir/foo.o""$(@D)"
"dir""$@""."

$(@F)
"$@""$@""dir/foo.o""$(@F)""foo.o""$(@F)"
"$(notdir $@)"
"$(*D)"
"$(*F)"
"$(*D)"
"dir""$(*F)""foo"
"$(%D)"
"$(%F)"
"archive(member)"
"member"
"$(<D)"
"$(<F)"

"$(^D)"
"$(^F)"

71 78

2005 10 14

Makefile

"$(+D)"
"$(+F)"

"$(?D)"
"$(?F)"

"$<"$
"$(<)""$<"
"""
"""

4
"%"
"%""%""%"
"""%.c""test.c""test"""
"%"""""

""
"e%t""src/eat""src/a"
""
"c%r""src/car"""

%.o : %.c
$(CC) -c $(CPPFLAGS) $(CFLAGS) -D$(date)

%.o : %.s
72 78

2005 10 14

Makefile

""

Makefile GNU make


""""
".c.o"
"%o : %c"".c"
"% :%.c"
make make
make
".c"".o" make ".c.o"
".c"".o"
.c.o:
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

.c.o: foo.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
".c.o""foo.h"
%.o: %.c foo.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

make ".SUFFIXES"

.SUFFIXES: .hack .win


.hack .win

73 78

2005 10 14

Makefile

.SUFFIXES: #
.SUFFIXES: .c .o .h #

make "-r""-no-builtin-rules"
"SUFFIXE"".SUFFIXES"
"SUFFIXE"

T T
Makefile
"archive(member)"
T"member" T
1 T D N
T "src/foo.o"
D "src/"N "foo.o"
2 T N
3"%"

4
5
1""SS T N "%"
2"%"""S
D
3

""
4

6 5

1
2 5
3
4
74 78

2005 10 14

Makefile

7".DEFAULT"".DEFAULT"
T

make
Object Unix
"ar"

archive(member)
"ar"

foolib(hack.o) : hack.o
ar cr foolib hack.o
member
foolib(hack.o kludge.o)

foolib(hack.o) foolib(kludge.o)
Shell
foolib(*.o)

make "a(m)"

75 78

2005 10 14

Makefile

"(m)""%.o"
"make foo.a(bar.o)" Makefile "bar.o"
bar.o make bar.c bar.o
make
cc -c bar.c -o bar.o
ar r foo.a bar.o
rm -f bar.o
"$%"
""

""""
.c.a:
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
$(AR) r $@ $*.o
$(RM) $*.o

(%.o)
$(CC)
$(AR)
$(RM)

: %.c
$(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
r $@ $*.o
$*.o

make "-j"
ar
make
"-j"

76 78

2005 10 14

Makefile


GNU make Makefile
make make
80% make"
" make make
GNU make VC nmakeBCB make UNIX
make Unix
make SCO Unix IBM AIX LinuxSolarisHP-UXAIX Alpha
Linux Solaris Unix make
Richard Stallman make cc/gcc
GNU make UNIX GNU GNU
GNU GNU
GNU Unix Windows""
make make
make Shell
Unix Makefile
tarawkmailsedcvscompresslsrmyaccrpmftp
"""""""""""
"

"@@@N###N"
make awk
sed"@@@N###N" C
" C" C "EXEC SQL" SQL
cc/gcc " C" cpre C

make
Unix
Makefile vi
"!make" vi Makefile
Makefile
Makefile
UNIX 400 200
Makefile

Makefile

77 78

2005 10 14

Makefile

GNU Makefile

Unix
/
P2PWeb ServiceJ2EE

MSNhaoel@hotmail.com
QQ753640
MSNhotmail

78 78

2005 10 14