跟我一起写Makefile (重制版) 含书签 文字版

Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

Makefile (PDF )

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 Windows Windows


integrated development environment IDE
makefile HTML
HTML Unix makefile
makefile

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

Object File Object File


Linker Error VC Link 2001
Object File

gnu make

2 Chapter 1.
Chapter 2

makefile

make makefile make

makefile gnu
make 8 c 3 makefile
make

1. c

2. c c

3. c

makefile make make

2.1 makefile

makefile makefile

target ... : prerequisites ...


recipe
...
...

target
object file label

prerequisites
target / target

recipe
target shell

3
, 1.0

target prerequisites
command :

prerequisites target recipe

makefile makefile

makefile makefile
makefile
:)

2.2

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
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
makefile Makefile make edit
make clean

makefile target edit *.o


prerequisites .c .h .o .o
edit

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

1. make Makefile makefile

2. target edit

3. edit edit .o edit


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

command.h kdb.o command.o files.o edit

2.3. make 5
, 1.0

2.4 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

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

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

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 .PHONY clean

2.6 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)

( )

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)

defs.h command.h buffer.h

makefile

.o

2.7

Makefile .o

clean:
rm edit $(objects)

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

.PHONY clean rm
clean
make clean

makefile makefile makefile

2.8 Makefile

Makefile

1. Makefile

2. make Make-
file make

8 Chapter 2. makefile
, 1.0

3. Makefile C
Makefile

4. Makefile Makefile C include


Makefile C #if

5. Makefile UNIX Shell # C/C++


// Makefile # \#

Makefile Tab

2.9 Makefile

make GNUmakefile makefile


Makefile Makefile
README GNUmakefile GNU make
make make makefile Makefile

Makefile Make.Solaris Make.Linux


Makefile make -f --file make -f Make.Solaris make
--file Make.Linux -f --file makefile

2.10 Makefile

Makefile include Makefile C #include


include

include <filenames>...

<filenames> Shell

include Tab include <filenames>


Makefile a.mk b.mk c.mk foo.make
$(bar) bish bash

include foo.make *.mk $(bar)

include foo.make a.mk b.mk c.mk bish bash

make include Makefile


C/C++ #include make
make

2.9. Makefile 9
, 1.0

1. make -I --include-dir make

2. <prefix>/include /usr/local/bin /usr/gnu/include


/usr/local/include /usr/include

.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

GNU make make

1. Makefile

2. include Makefile

3.

4.

5.

6.

7.

1-5 6-7 make


make make

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

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


cc -c -g foo.c

foo.o foo.c defs.h


cc -c -g foo.c Tab

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


foo.o foo.o

2. foo.o cc foo.o foo.c


include defs.h

3.2

targets : prerequisites
command
...

13
, 1.0

targets : prerequisites ; command


command
...

targets

command target:prerequisites Tab


prerequisites

prerequisites

\ make
make

make UNIX Shell /bin/sh

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

objects := $(wildcard *.o)

1. .c

objects := $(wildcard *.c)

2. (1) .o 3 make :

$(patsubst %.c,%.o,$(wildcard *.c))

3. (1)(2) .c .o

objects := $(patsubst %.c,%.o,$(wildcard *.c))


foo : $(objects)
cc -o foo $(objects)

wildcard patsubst Makefile

3.4

make
make make

Makefile VPATH make


make

VPATH = src:../headers

src ../headers make

3.4. 15
, 1.0

make vpath
make VPATH

vpath <pattern> <directories>


<pattern> <directories>

vpath <pattern>
<pattern>

vpath

vpath <pattern> % % %
\ %.h .h <pattern> <directories>
< pattern>

vpath %.h ../headers

make ../headers .h

vpath vpath
<pattern> <pattern> make vpath

vpath %.c foo


vpath % blish
vpath %.c bar

.c foo blish bar

vpath %.c foo:bar


vpath % blish

.c foo bar blish

3.5

clean

clean:
rm *.o temp

clean
make clean

clean
make

16 Chapter 3.
, 1.0

.PHONY
make

.PHONY : clean

clean clean make clean

.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
all
.PHONY : all all
.PHONY : all make all
make all

.PHONY : cleanall cleanobj cleandiff

cleanall : cleanobj cleandiff


rm program

cleanobj :
rm *.o

cleandiff :
rm *.diff

3.5. 17
, 1.0

make cleanall cleanobj cleandiff


make cleanall make cleanobj make cleandiff

3.6

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


$@
$@

3.7

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


<commands>
...

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

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.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

files = foo.elc bar.o lose.o

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


$(CC) -c $(CFLAGS) $< -o $@
$(filter %.elc,$(files)): %.elc: %.el
emacs -f batch-byte-compile $<

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


Makefile

3.8

Makefile main.c
#include "defs.h"

main.o : main.c defs.h

C
Makefile
C/C++ C/C++ -M
:

3.8. 19
, 1.0

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
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

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

3.8. 21
, 1.0

22 Chapter 3.
Chapter 4

Shell make
Tab
Tab make

UNIX Shell make /bin/sh UNIX


Shell Shell Makefile # C/C++ //

4.1

make @
make :

@echo XXX ......

make XXX @
make :

echo XXX ......


XXX ......

make make -n --just-print


Makefile

make -s --silent --quiet

23
, 1.0

4.2

make

cd cd

exec:
cd /home/hchen
pwd

exec:
cd /home/hchen; pwd

make exec cd pwd Makefile


cd pwd /home/hchen

make SHELL Shell UNIX


Shell /bin/sh MS-DOS MS-DOS SHELL
UNIX make SHELL
PATH
MS-DOS
.exe .com .bat .sh

4.3

make make

make

mkdir
mkdir mkdir
mkdir

Makefile - Tab

clean:
-rm -f *.o

make -i --ignore-errors Makefile


.IGNORE

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 Makefile Makefile


Makefile -e

Makefile :

export <variable ...>;

Makefile :

unexport <variable ...>;

export variable = value

variable = value
export variable

export variable := value

4.4. make 25
, 1.0

variable := value
export variable

export variable += value

variable += value
export variable

export

SHELL MAKEFLAGS export


Makefile MAKEFLAGS make
Makefile make Makefile MAKEFLAGS
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/make
make -w :

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

26 Chapter 4.
, 1.0

4.5

Makefile
define endef :

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

run-yacc Makefile define endef


Yacc Yacc y.tab.c

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

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

4.5. 27
, 1.0

28 Chapter 4.
Chapter 5

Makefile C/C++ Makefile


C/C++ Makefile
Makefile Makefile

: # =
foo Foo FOO
Makefile MakeFlags

$< $@

5.1

$ ()
{} $ $$

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)

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)

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 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

y bar foo 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
#
# #

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

dir /foo/bar 4
$(dir)/file

?=

5.2. 31
, 1.0

FOO ?= bar

FOO FOO bar FOO

ifeq ($(origin FOO), undefined)


FOO = bar
endif

5.3

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

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

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

$(x) y $($(x)) $(y) $(a) z


x=y x=$(y)

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

32 Chapter 5.
, 1.0

$(a) u

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

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


Hello

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

$($($(z))) $($(y)) $($(subst 1,2,$(x))) $(x)


variable1 subst variable1 1 2 variable1
variable2 $(a) $(variable2) Hello

first_second = Hello
a = first
b = second
all = $($a_$b)

$a_$b first_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
( )

5.3. 33
, 1.0

( )

bar := a d b g q c

foo := $($(func) $(bar))

do_sort foo := $(sort a d b g q c) $(foo)


abcdgq do_sort foo := $(strip a d b g q c)
strip

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

dir foo_sources foo_print

5.4

+=

objects = main.o foo.o bar.o utils.o


objects += another.o

$(objects) main.o foo.o bar.o utils.o another.o another.o

+=

objects = main.o foo.o bar.o utils.o


objects := $(objects) another.o

+=

+= = +=
:= += :=

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 :

override <variable>; = <value>;

override <variable>; := <value>;

override <variable>; += <more text>;

define define override :

override define foo


bar
endef

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

make make Makefile Makefile


make
make -e Makefile

CFLAGS Makefile
Makefile CFLAGS
Makefile

make Makefile
Makefile
Makefile export

Makefile

5.8

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

foo.o : foo.c
( )

36 Chapter 5.
, 1.0

( )

$(CC) $(CFLAGS) foo.c

bar.o : bar.c
$(CC) $(CFLAGS) bar.c

$(CFLAGS) prog
prog.o foo.o bar.o $(CFLAGS) -g

5.9

GNU make Pattern-specific Variable

make % .o

%.o : CFLAGS = -O

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

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

override make

5.9. 37
, 1.0

38 Chapter 5.
Chapter 6

make

6.1

$(CC) gcc GNU

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)

ifeq else endif ifeq


else
endif endif

$(CC) gcc foo

foo: $(objects)
$(CC) -o foo $(objects) $(libs_for_gcc)

$(CC) gcc cc foo

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

ifeq (<arg1>, <arg2>)


ifeq '<arg1>' '<arg2>'
ifeq "<arg1>" "<arg2>"
ifeq "<arg1>" '<arg2>'
ifeq '<arg1>' "<arg2>"

arg1 arg2 make :

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


<text-if-empty>
endif

strip Empty <text-if-empty>

ifneq

40 Chapter 6.
, 1.0

ifneq (<arg1>, <arg2>)


ifneq '<arg1>' '<arg2>'
ifneq "<arg1>" "<arg2>"
ifneq "<arg1>" '<arg2>'
ifneq '<arg1>' "<arg2>"

arg1 arg2 ifeq

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>}

<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

43
, 1.0

7.2

7.2.1 subst

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

• <text> <from> <to>

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

feet on the street ee EE fEEt on the strEEt

7.2.2 patsubst

$(patsubst <pattern>,<replacement>,<text>)

• <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))

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> <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

7.2. 45
, 1.0

7.2.6 filter-out

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

• <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

7.2.7 sort

$(sort <list>)

• <list>

• $(sort foo bar lose) bar foo lose

• sort <list>

7.2.8 word

$(word <n>,<text>)

• <text> <n>

• <text> <n> <n> <text>

• $(word 2, foo bar baz) bar

46 Chapter 7.
, 1.0

7.2.9 wordlist

$(wordlist <ss>,<e>,<text>)

• <text> <ss> <e> <ss> <e>

• <text> <ss> <e> <ss> <text>


<e> <text> <ss> <text>

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

7.2.10 words

$(words <text>)

• <text>

• <text>

• $(words, foo bar baz) 3

• <text> $(word $(words <text>),


<text>)

7.2.11 firstword

$(firstword <text>)

• firstword

• <text>

• <text>

• $(firstword foo bar) foo

• word $(word 1,<text>)

make VPATH
CFLAGS

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

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


-Isrc -I../headers cc gcc

7.2. 47
, 1.0

7.3

7.3.1 dir

$(dir <names...>)

• dir

• <names> /
./

• <names>

• $(dir src/foo.c hacks) src/ ./

7.3.2 notdir

$(notdir <names...>)

• notdir

• <names> /

• <names>

• : $(notdir src/foo.c hacks) foo.c hacks

7.3.3 suffix

$(suffix <names...>)

• suffix

• <names>

• <names>

• $(suffix src/foo.c src-1.0/bar.c hacks) .c .c

48 Chapter 7.
, 1.0

7.3.4 basename

$(basename <names...>)

• basename

• <names>

• <names>

• $(basename src/foo.c src-1.0/bar.c hacks) src/foo src-1.0/bar hacks

7.3.5 addsuffix

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

• addsuffix

• <suffix> <names>

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

7.3.6 addprefix

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

• addprefix

• <prefix> <names>

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

7.3.7 join

$(join <list1>,<list2>)

• join

• <list2> <list1> <list1>


<list2> <list1> <list2>
<list1> <list2> <list1>

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

7.3. 49
, 1.0

7.4 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

7.5 if

if GNU make ifeq 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>)

make <expression> $(1) $(2) <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

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

contents := $(shell cat foo)


files := $(shell echo *.c)

Shell Makefile
Makefile
shell

7.9 make

make make Makefile


make

$(error <text ...>)

<text ...> error

52 Chapter 7.
, 1.0

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

ERR = $(error found an error!)

.PHONY: err

err: $(ERR)

ERROR_001 error err


error

$(warning <text ...>)

error make make

7.9. make 53
, 1.0

54 Chapter 7.
Chapter 8

make

make make makefile


make
make

8.1 make

make

1
make 1

2
make -q make 2

Make

8.2 Makefile

GNU make Makefile GNU-


makefile makefile Makefile

make Makefile make


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

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

sources = foo.c bar.c


ifneq ( $(MAKECMDGOALS),clean)
include $(sources:.c=.d)
endif

make clean makefile foo.d


bar.d makefile

.PHONY: all
all: prog1 prog2 prog3 prog4

makefile prog1 prog2 prog3


prog4 make all all
make make prog2 prog2

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:

• check test: makefile

makefile GNU
GNU UNIX

makefile
makefile

8.4

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

8.5 make

GNU make 3.80 make


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

• 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>, --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
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

-s, --silent, --quiet

-S, --no-keep-going, --stop


-k make 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

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

foo : foo.o bar.o


cc o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)

Makefile foo.o bar.o


make

make
make .o .c

61
, 1.0

C cc c $(CFLAGS) foo.c foo.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.p Pascal foo.c


foo.c C foo.o
Pascal C make foo.o C

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

<n>.o <n>.c $(CC) c $(CPPFLAGS)


$(CFLAGS)

2. C++

<n>.o <n>.cc <n>.cpp <n>.C $(CXX)


c $(CPPFLAGS) $(CXXFLAGS) .cc .cpp C++ .C

3. Pascal

62 Chapter 9.
, 1.0

<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)

• .r $(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 <n>.mod $(M2C)
$(M2FLAGS) $(MODFLAGS)

7.

<n>.o <n>.s as $ (AS)


$(ASFLAGS) <n>.s <n>.S C cpp
$(CPP) $(CPPFLAGS)

8. Object

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

x : y.o z.o

x.c y.c z.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

9. Yacc C

9.2. 63
, 1.0

<n>.c n.y Yacc $(YACC) $(YFALGS)


Yacc

10. Lex C

<n>.c n.l Lex $(LEX) $(LFALGS)


Lex

11. Lex Ratfor

<n>.r n.l Lex $(LEX) $(LFALGS)

12. C Yacc Lex Lint

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


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

9.3

makefile
make
make -R --no
builtin-variables

C $(CC) c $(CFLAGS) $(CPPFLAGS)


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

CC
CFLAGS

9.3.1

• AR : ar

• AS : as

• CC : C cc

• CXX : C++ g++

• CO : RCS co

• CPP : C $(CC) E

• FC : Fortran Ratfor f77

• GET : SCCS get

• LEX : Lex C Ratfor lex

• PC : Pascal pc

• YACC : Yacc C yacc

64 Chapter 9.
, 1.0

• 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 C ctangle

• RM : rm f

9.3.2

• 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

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

Make foo.c foo


make foo.o foo cc
cc o foo foo.c

9.5

% % %
%

% make
Makefile %

9.5.1

% %
% %.c .c
3 s.%.c s. .c 5

% % %
% %

%.o : %.c ; <command ......>;

.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 $@

$@ $<

%.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

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

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)

$(+D), $(+F)

$(?D), $(?F)

$< $
$(<) $<

68 Chapter 9.
, 1.0

9.5.4

% %
% % %.c
test.c test %

e%t src/eat src/a


c%r src/car

9.5.5

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

%.o : %.s

9.6

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 $@ $<

9.6. 69
, 1.0

.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

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

make -r -no-builtin-rules SUFFIXE


.SUFFIXES SUFFIXE

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

74 Chapter 10. make


Chapter 11

GNU make Makefile


make make
80% make
make make GNU
make VC nmake BCB make UNIX make
Unix make SCO Unix IBM AIX Linux
Solaris HP-UX AIX 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 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

MSN [email protected] QQ 753640 MSN


hotmail

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

2. Thomas Bushnell GNU Hurd

3. GNU C library

4. GNU Emacs

76 Chapter 11.

You might also like