跟我一起写Makefile (重制版) 含书签 文字版
跟我一起写Makefile (重制版) 含书签 文字版
跟我一起写Makefile (重制版) 含书签 文字版
2024 05 03
1 1
1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 makefile 3
2.1 makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.6 makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.8 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.9 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.10 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.11 MAKEFILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.12 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 13
3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4 23
4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.4 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5 29
5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
i
5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.5 override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6 39
6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7 43
7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
7.2.1 subst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
7.2.2 patsubst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
7.2.3 strip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
7.2.4 findstring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
7.2.5 filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
7.2.6 filter-out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
7.2.7 sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
7.2.8 word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
7.2.9 wordlist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.2.10 words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.2.11 firstword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.3.1 dir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.3.2 notdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.3.3 suffix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
7.3.4 basename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.3.5 addsuffix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.3.6 addprefix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.3.7 join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7.4 foreach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
7.5 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
7.6 call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.7 origin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.8 shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7.9 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
8 make 55
8.1 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8.2 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
ii
8.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
8.5 make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9 61
9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
9.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
9.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
9.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
9.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
9.5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
9.5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
9.5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
9.5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10 make 73
10.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11 75
iii
iv
Chapter 1
makefile
makefile
makefile Shell
makefile make
make makefile
IDE Delphi make Visual C++ nmake Linux
GNU make makefile
makefile make
GNU make
RedHat Linux 8.0 make 3.80 make
IEEE 1003.2-1992 POSIX.2
C/C++ C/C++
UNIX GCC CC
1.1
C C++
Windows .obj UNIX .o Object File
compile Object File link
C/C++
1
, 1.0
.o
.obj
.o .obj
Object File
Windows
Library File .lib UNIX Archive File .a
gnu make
2 Chapter 1.
Chapter 2
makefile
makefile gnu
make 8 c 3 makefile
make
1. c
2. c c
3. c
2.1 makefile
makefile makefile
target
object file label
prerequisites
target / target
recipe
target shell
3
, 1.0
target prerequisites
command :
makefile makefile
makefile makefile
makefile
:)
2.2
3 8 C
makefile
\ makefile
makefile Makefile make edit
make clean
4 Chapter 2. makefile
, 1.0
recipe Tab
make make targets
prerequisites prerequisites targets
target make
clean C label
make
make label
makefile
2.3 make
make
2. target edit
4. edit .o make .o
.o
5. C make .o .o make
edit
make make
make
make make
clean
make make clean
file.c
file.o
file.o file.o edit edit
edit
2.3. make 5
, 1.0
2.4 makefile
edit
.o .o
clean makefile
makefile
makefile makefile makefile
C
makefile $(objects)
makefile
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
6 Chapter 2. makefile
, 1.0
2.5 make
GNU make
.o make
make .o .c make
whatever.o whatever.c whatever.o cc -c whatever.c
makefile makefile
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)
2.6 makefile
make .o .h
.h make
makefile
edit : $(objects)
cc -o edit $(objects)
( )
2.5. make 7
, 1.0
( )
$(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)
makefile
.o
2.7
Makefile .o
clean:
rm edit $(objects)
.PHONY : clean
clean :
-rm edit $(objects)
.PHONY clean rm
clean
make clean
2.8 Makefile
Makefile
1. Makefile
2. make Make-
file make
8 Chapter 2. makefile
, 1.0
3. Makefile C
Makefile
Makefile Tab
2.9 Makefile
2.10 Makefile
include <filenames>...
<filenames> Shell
2.9. Makefile 9
, 1.0
.INCLUDE_DIRS make -I
make
make
makefile make
make make
include -
-include <filenames>...
include make
sinclude -include
2.11 MAKEFILES
MAKEFILES make
include Makefile include
Makefile make
make
Makefile
Makefile
2.12 make
1. Makefile
2. include Makefile
3.
4.
5.
6.
7.
10 Chapter 2. makefile
, 1.0
make
2.12. make 11
, 1.0
12 Chapter 2. makefile
Chapter 3
Makefile Makefile
make Makefile
make
3.1
3.2
targets : prerequisites
command
...
13
, 1.0
targets
prerequisites
\ make
make
3.3
make
* ? ~ Unix B-Shell
~ ~/test $HOME
test ~hchen/test hchen test Unix
make Windows MS-DOS
HOME
*.c c
* \ \* *
clean:
rm -f *.o
clean: main.c
cat
clean:
cat main.c
rm -f *.o
Shell
14 Chapter 3.
, 1.0
print: *.c
lpr -p $?
touch print
print .c $?
objects = *.o
*.o objects
*.o Makefile C/C++ objects
.o
1. .c
2. (1) .o 3 make :
3. (1)(2) .c .o
3.4
make
make make
VPATH = src:../headers
3.4. 15
, 1.0
make vpath
make VPATH
vpath <pattern>
<pattern>
vpath
vpath <pattern> % % %
\ %.h .h <pattern> <directories>
< pattern>
make ../headers .h
vpath vpath
<pattern> <pattern> make vpath
3.5
clean
clean:
rm *.o temp
clean
make clean
clean
make
16 Chapter 3.
, 1.0
.PHONY
make
.PHONY : clean
.PHONY : clean
clean :
rm *.o temp
Makefile
make Makefile
prog2 : prog2.o
cc -o prog2 prog2.o
Makefile all
all
all
.PHONY : all all
.PHONY : all make all
make all
cleanobj :
rm *.o
cleandiff :
rm *.diff
3.5. 17
, 1.0
3.6
Makefile
$@
bigoutput : text.g
generate text.g -big > bigoutput
littleoutput : text.g
generate text.g -little > littleoutput
3.7
targets
target-pattern targets
prereq-patterns target-pattern
<target-
pattern> %.o <target>; .o <prereq-
patterns> %.c <target-pattern>
<target-pattern> % .o .c
% %
\ %
18 Chapter 3.
, 1.0
all: $(objects)
$object %.o .o
foo.o bar.o $object %.c %.o % foo bar
.c foo.c bar.c $< $@
$< $@ foo.o bar.o
foo.o : foo.c
$(CC) -c $(CFLAGS) foo.c -o foo.o
bar.o : bar.c
$(CC) -c $(CFLAGS) bar.c -o bar.o
%.o
3.8
Makefile main.c
#include "defs.h"
C
Makefile
C/C++ C/C++ -M
:
3.8. 19
, 1.0
cc -M main.c
gcc -M main.c :
Makefile Makefile
Makefile
GNU
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
20 Chapter 3.
, 1.0
.d .d
.d
Makefile Makefile
include Makefile
include $(sources:.c=.d)
3.8. 21
, 1.0
22 Chapter 3.
Chapter 4
Shell make
Tab
Tab make
4.1
make @
make :
make XXX @
make :
23
, 1.0
4.2
make
cd cd
exec:
cd /home/hchen
pwd
exec:
cd /home/hchen; pwd
4.3
make make
make
mkdir
mkdir mkdir
mkdir
Makefile - Tab
clean:
-rm -f *.o
24 Chapter 4.
, 1.0
make -k --keep-going
4.4 make
Makefile Makefile
Makefile Makefile
subdir Makefile
Makefile
subsystem:
cd subdir && $(MAKE)
subsystem:
$(MAKE) -C subdir
$(MAKE) make
subdir make
Makefile :
Makefile :
variable = value
export variable
4.4. make 25
, 1.0
variable := value
export variable
variable += value
export variable
export
subsystem:
cd subdir && $(MAKE) MAKEFLAGS=
MAKEFLAGS -t ,
-n -q
-w --print-directory make
make /home/hchen/gnu/make
make -w :
make :
26 Chapter 4.
, 1.0
4.5
Makefile
define endef :
define run-yacc
yacc $(firstword $^)
mv y.tab.c $@
endef
foo.c : foo.y
$(run-yacc)
run-
yacc $^ foo.y $@ foo.c $ make
4.5. 27
, 1.0
28 Chapter 4.
Chapter 5
: # =
foo Foo FOO
Makefile MakeFlags
$< $@
5.1
$ ()
{} $ $$
$(objects) : defs.h
C/C++
foo = c
prog.o : prog.$(foo)
$(foo)$(foo) -$(foo) prog.$(foo)
29
, 1.0
prog.o : prog.c
cc -c prog.c
Makefile Makefile
5.2
Makefile
= =
foo = $(bar)
bar = $(ugh)
ugh = Huh?
all:
echo $(foo)
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
CFLAGS = $(CFLAGS) -O
A = $(B)
B = $(A)
make make
make
make wildcard shell
30 Chapter 5.
, 1.0
make
:=
x := foo
y := $(x) bar
x := later
y := foo bar
x := later
y := $(x) bar
x := foo
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
dir /foo/bar 4
$(dir)/file
?=
5.2. 31
, 1.0
FOO ?= bar
5.3
$(var:a=b) ${var:a=b}
var a a b
$(foo) $(foo) .o
.c $(bar) a.c b.c c.c
% $(bar)
a.c b.c c.c
x = y
y = z
a := $($(x))
x = y
y = z
z = u
a := $($($(x)))
32 Chapter 5.
, 1.0
$(a) u
x = $(y)
y = z
z = Hello
a := $($(x))
x = variable1
variable2 := Hello
y = $(subst 1,2,$(x))
z = y
a := $($($(z)))
first_second = Hello
a = first
b = second
all = $($a_$b)
sources := $($(a1)_objects:.o=.c)
ifdef do_sort
func := sort
else
func := strip
endif
( )
5.3. 33
, 1.0
( )
bar := a d b g q c
dir = foo
$(dir)_sources := $(wildcard $(dir)/*.c)
define $(dir)_print
lpr $($(dir)_sources)
endef
5.4
+=
+=
+=
+= = +=
:= += :=
variable := value
variable += more
variable := value
variable := $(variable) more
34 Chapter 5.
, 1.0
variable = value
variable += more
= += =
make
5.5 override
make Makefile
Makefile override :
5.6
define define
define endef
= [Tab]
define Tab make
define :
define two-lines
echo foo
echo $(bar)
endef
5.5. override 35
, 1.0
5.7
CFLAGS Makefile
Makefile CFLAGS
Makefile
make Makefile
Makefile
Makefile export
Makefile
5.8
Makefile
$<
Target-specific Variable
<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
foo.o : foo.c
( )
36 Chapter 5.
, 1.0
( )
bar.o : bar.c
$(CC) $(CFLAGS) bar.c
$(CFLAGS) prog
prog.o foo.o bar.o $(CFLAGS) -g
5.9
make % .o
%.o : CFLAGS = -O
override make
5.9. 37
, 1.0
38 Chapter 5.
Chapter 6
make
6.1
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)
foo: $(objects)
$(CC) -o foo $(objects) $(libs_for_gcc)
foo: $(objects)
$(CC) -o foo $(objects) $(normal_libs)
39
, 1.0
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)
6.2
<conditional-directive>
<text-if-true>
endif
<conditional-directive>
<text-if-true>
else
<text-if-false>
endif
<conditional-directive> ifeq
ifeq
ifneq
40 Chapter 6.
, 1.0
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) yes no
ifndef
ifndef <variable-name>
ifdef
<conditional-directive> Tab
# else endif Tab
make Makefile
$@
6.2. 41
, 1.0
make
42 Chapter 6.
Chapter 7
Makefile make
7.1
$(<function> <arguments>)
${<function> <arguments>}
comma:= ,
empty:=
space:= $(empty) $(empty)
foo:= a b c
bar:= $(subst $(space),$(comma),$(foo))
43
, 1.0
7.2
7.2.1 subst
$(subst <from>,<to>,<text>)
7.2.2 patsubst
$(patsubst <pattern>,<replacement>,<text>)
• <text> Tab
<pattern> <replacement> <pattern> %
<replacement> % <replacement> %
<pattern> % \ \% %
• $(var:<pattern>=<replacement>;
) $(patsubst <pattern>,<replacement>,$(var)) $(var:
<suffix>=<replacement>) $(patsubst %<suffix>,%<replacement>,$(var))
44 Chapter 7.
, 1.0
7.2.3 strip
$(strip <string>)
• <string>
$(strip a b c )
a b c
7.2.4 findstring
$(findstring <find>,<in>)
• <in> <find>
• <find>
$(findstring a,a b c)
$(findstring a,b c)
7.2.5 filter
$(filter <pattern...>,<text>)
• <pattern>
7.2. 45
, 1.0
7.2.6 filter-out
$(filter-out <pattern...>,<text>)
• <pattern>
7.2.7 sort
$(sort <list>)
• <list>
• sort <list>
7.2.8 word
$(word <n>,<text>)
• <text> <n>
46 Chapter 7.
, 1.0
7.2.9 wordlist
$(wordlist <ss>,<e>,<text>)
7.2.10 words
$(words <text>)
• <text>
• <text>
7.2.11 firstword
$(firstword <text>)
• firstword
• <text>
• <text>
make VPATH
CFLAGS
7.2. 47
, 1.0
7.3
7.3.1 dir
$(dir <names...>)
• dir
• <names> /
./
• <names>
7.3.2 notdir
$(notdir <names...>)
• notdir
• <names> /
• <names>
7.3.3 suffix
$(suffix <names...>)
• suffix
• <names>
• <names>
48 Chapter 7.
, 1.0
7.3.4 basename
$(basename <names...>)
• basename
• <names>
• <names>
7.3.5 addsuffix
$(addsuffix <suffix>,<names...>)
• addsuffix
• <suffix> <names>
7.3.6 addprefix
$(addprefix <prefix>,<names...>)
• addprefix
• <prefix> <names>
7.3.7 join
$(join <list1>,<list2>)
• join
7.3. 49
, 1.0
7.4 foreach
$(foreach <var>,<list>,<text>)
<list> <var>
<text> <text> <text>
<text>
foreach
names := a b c d
7.5 if
$(if <condition>,<then-part>)
$(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>
50 Chapter 7.
, 1.0
7.6 call
call
call
$(call <expression>,<parm1>,<parm2>,...,<parmn>)
foo a b
foo b a
call call 2
call
7.7 origin
origin
$(origin <variable>)
<variable> <variable>
$ Origin origin
:
undefined
<variable> origin undefined
default
<variable> CC
environment
<variable> Makefile -e
file
<variable> Makefile
7.6. call 51
, 1.0
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
7.8 shell
shell Shell ‘
shell
awk sed
Shell Makefile
Makefile
shell
7.9 make
52 Chapter 7.
, 1.0
ifdef ERROR_001
$(error error is $(ERROR_001))
endif
.PHONY: err
err: $(ERR)
7.9. make 53
, 1.0
54 Chapter 7.
Chapter 8
make
8.1 make
make
1
make 1
2
make -q make 2
Make
8.2 Makefile
make f hchen.mk
55
, 1.0
make -f makefile
make
8.3
make makefile
make makefile
make make
make clean
makefile - =
make make
make MAKECMDGOALS
.PHONY: all
all: prog1 prog2 prog3 prog4
make makefile
makefile Unix GNU
makefile
makefile
• all:
• clean: make
• install:
• print:
• tar: tar
56 Chapter 8. make
, 1.0
• dist: tar Z gz
• TAGS:
makefile GNU
GNU UNIX
makefile
makefile
8.4
makefile
make
makefile
-t, --touch
make
-q, --question
-p -v makefile
8.5 make
-b, -m
make
-B, --always-make
-C <dir>, --directory=<dir>
makefile -C make
make -C ~hchen/test -C prog make -C
~hchen/test/prog
8.4. 57
, 1.0
-debug[=<options>]
make
<options>
• a: all
• b: basic
• v: verbose b makefile
• i: implicit
• j: jobs PID
-d
debug=a
-e, --environment-overrides
makefile
-h, --help
-i , --ignore-errors
-I <dir>, --include-dir=<dir>
makefile -I
-j [<jobsnum>], --jobs[=<jobsnum>]
make
-j -j MS-DOS
-k, --keep-going
-p, --print-data-base
makefile makefile
makefile make -qp
makefile make p f /dev/null
58 Chapter 8. make
, 1.0
makefile makefile
-q, --question
0
2
-r, --no-builtin-rules
make
-R, --no-builtin-variabes
make
-t, --touch
UNIX touch
-v, --version
make make
-w, --print-directory
makefile make
--no-print-directory
-w
--warn-undefined-variables
make
8.5. make 59
, 1.0
60 Chapter 8. make
Chapter 9
Makefile
C/C++ Unix .o Windows .obj
Makefile
make Makefile
.c .o make
.o
CFLAGS
Makefile
Makefile
9.1
make
make
make
Makefile
make
make .o .c
61
, 1.0
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
9.2
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
1. C
2. C++
3. Pascal
62 Chapter 9.
, 1.0
4. Fortran/Ratfor
• .f $(FC) c $(FFLAGS)
5. Fortran/Ratfor
6. Modula-2
7.
8. Object
<n> <n>.o C ld
$(CC) $(LDFLAGS) <n>.o $(LOADLIBES) $(LDLIBS)
Object :
x : y.o z.o
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
9. Yacc C
9.2. 63
, 1.0
10. Lex C
9.3
makefile
make
make -R --no
builtin-variables
CC
CFLAGS
9.3.1
• AR : ar
• AS : as
• CC : C cc
• CO : RCS co
• CPP : C $(CC) E
• PC : Pascal pc
64 Chapter 9.
, 1.0
• RM : rm f
9.3.2
• ARFLAGS : AR rv
• ASFLAGS : .s .S
• CFLAGS : C
• CXXFLAGS : C++
• COFLAGS : RCS
• CPPFLAGS : C C Fortran
• FFLAGS : Fortran
• LDFLAGS : ld
• LFLAGS : Lex
• PFLAGS : Pascal
• YFLAGS : Yacc
9.4
.o
Yacc [.y] .c C
.c C .c
.y Yacc .c C
.c .o
9.4. 65
, 1.0
.c make
makefile
rm -f
makefile
.INTERMEDIATE .INTERMEDIATE :
mid
make .SECONDARY
.SECONDARY : sec %.o .PRECIOUS
make
9.5
% % %
%
% make
Makefile %
9.5.1
% %
% %.c .c
3 s.%.c s. .c 5
% % %
% %
.c .o a.o b.o
%c a.c b.c
% make
make
66 Chapter 9.
, 1.0
make make
9.5.2
, .c .o .
%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
$@ $<
9.5.3
• $@ : $@
• $% : foo.a(bar.o)
$% bar.o $@ foo.a Unix .a Windows
.lib
• $< : % $<
• $? :
• $^ :
• $+ : $^
9.5. 67
, 1.0
• $* : % dir/a.foo.b
a.%.b $* dir/foo
$* make
$* foo.c .c make
$* foo GNU make make
$* make
$*
$?
lib object object Makefile
$@ $< $% $*
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)
$(+D), $(+F)
$(?D), $(?F)
$< $
$(<) $<
68 Chapter 9.
, 1.0
9.5.4
% %
% % %.c
test.c test %
9.5.5
%.o : %.c
$(CC) -c $(CPPFLAGS) $(CFLAGS) -D$(date)
%.o : %.s
9.6
.c.o %o :
%c .c % : %.c
make make
make .c .o
make .c.o .c
.o
.c.o:
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
9.6. 69
, 1.0
.c.o: foo.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
.c.o foo.h
make .SUFFIXES
.hack .win
.SUFFIXES: #
.SUFFIXES: .c .o .h #
9.7
T T
Makefile
archive(member) T
member T
1. T D N T src/foo.o D
src/ N foo.o
2. T N T
N
3. %
4.
5.
1. S S T N %
2. % S
D
70 Chapter 9.
, 1.0
3.
4.
6. 5
1.
2. 5
3.
4.
5.
6. .DEFAULT .DEFAULT T
9.7. 71
, 1.0
72 Chapter 9.
Chapter 10
make
Object Unix
ar
10.1
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)
73
, 1.0
10.2
make a(m)
(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
$%
10.3
.c.a:
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
$(AR) r $@ $*.o
$(RM) $*.o
(%.o) : %.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
$(AR) r $@ $*.o
$(RM) $*.o
10.4
make -j ar
make
-j
make make
make Shell Unix
Makefile tar awk mail sed cvs
compress ls rm yacc rpm ftp
@@@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
75
, 1.0
Makefile
GNU Makefile
Unix
/
P2P Web Service J2EE
make
Richard Stallman
Windows
http://www.stallman.org/
http://bbs.chinaunix.net/attachments/rms.jpg
Roland McGrath
http://www.frob.com/~roland/
1. GNU make
3. GNU C library
4. GNU Emacs
76 Chapter 11.