Do coding yourself now! Learn real Unix/Linux system by realizing every sample.
Make是最常用的构建工具,诞生于1977年,主要用于C语言的项目。实际上 ,只要某个文件有变化,就需要重新构建的项目,都可以用Make构建。
Makefile文件的格式
target : prerequisites
recipe
…
井号#
在Makefile中表示注释。
正常情况下,make会打印每条命令,然后再执行,这就叫做回声(echoing)。
在命令的前面加上@
,就可以关闭回声。
test :
@# 这是测试
@echo TODO
Makefile的通配符与Bash一致,主要有星号*
, 问号?
和 []
。比如, *.o
表示所有后缀名为o的文件。
clean :
rm -f *.o
For example,
%.c
as a pattern matches any file name that ends in.c
.s.%.c
as a pattern matches any file name that starts withs.
, ends in.c
and is at least five characters long. (There must be at least one character to match the%
.) The substring that the%
matches is called the stem. Make命令允许对文件名,进行类似正则运算的匹配,主要用到的匹配符是%。比如,假定当前目录下有 f1.c 和 f2.c 两个源码文件,需要将它们编译为对应的对象文件。
%.o : %.c
等同于
f1.o : f1.c
f2.o : f2.c
-
VARIABLE = value # 在执行时扩展,允许递归扩展。
-
VARIABLE := value # 在定义时扩展。
-
VARIABLE ?= value # 只有在该变量为空时才设置值。
-
VARIABLE += value # 将值追加到变量的尾端。
-
AR Archive-maintaining program; default ‘ar’.
-
AS Program for compiling assembly files; default ‘as’.
-
CC Program for compiling C programs; default ‘cc’.
-
CFLAGS Extra flags to give to the C compiler.
-
LDFLAGS Extra flags to give to compilers when they are supposed to invoke the linker,
ld
, such as-L
. Libraries (-lfoo) should be added to the LDLIBS variable instead. -
LDLIBS Library flags or names given to compilers when they are supposed to invoke the linker,
ld
. LOADLIBES is a deprecated (but still supported) alternative to LDLIBS. Non-library linker flags, such as -L, should go in the LDFLAGS variable.
-
$@
The file name of the target of the rule. -
$<
The name of the first prerequisite. -
$^
The names of all the prerequisites, with spaces between them. -
$?
The names of all the prerequisites that are newer than the target, with spaces between them. -
$(@D)
The directory part of the file name of the target, with the trailing slash removed. Also$(<D)
. -
$(@F)
The file-within-directory part of the file name of the target.If the value of$@
is dir/foo.o then$(@F)
is foo.o.$(@F)
is equivalent to$(notdir $@)
. Also$(<F)
Makefile使用 Bash 语法,完成判断和循环。
CC = gcc
ifeq ($(CC),gcc)
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif
LIST = one two three
loop :
for item in $(LIST); don \
echo $$item; \
done
Makefile 还可以使用函数,格式如下:
$(function arguments)
# or
${function arguments}
Makefile提供了许多内置函数,可供调用。下面是几个常用的内置函数。
-
shell 函数
srcfiles := $(shell echo src/{00..99}.txt)
-
wildcard 函数
srcfiles := $(wildcard src/*.txt)
-
subst 函数
$(subst from,to,text)
-
patsubst函数
$(patsubst pattern,replacement,text)
-
替换后缀名
$(OUTPUT:.js=.min.js)
-
Make 命令教程 - 阮一峰
-
GNU Make Manual - Official Manual
-
跟我一起写Makefile - 陈皓