三个概念:
- 目标(target):指要干什么
- 依赖关系(dependency):指目标所依赖的其他目标
- 命令(command):指告诉 make 如何生成目标
规则的语法:
targets:prerequisite
command
....
第一个小程序:
all :
echo “hello world”
- 目标:all
- 先决条件:无
- 命令:echo “hello world”
$ make
echo “hello world”
hello world
$ make all
echo “hello world”
hello world
第一种运行方式是
make 不带任何参数,可以看到,最终在终端上输出两行:
第一行:make 打印出来的将要运行的命令
第二行:命令的运行结果
第二种运行方式是
make 带参数 all,告诉 make 我们希望构建 all 目标,结果同上;
改动一:
all :
echo “hello world”
test
:
echo “just for test”
- 目标:all 、test
- 先决条件:无
- 命令:echo “hello world” 、echo “just for test”
$ make
echo “hello world”
hello world
$ make test
echo “just for test”
just for test
从运行结果中:
- 一个 Makefile 中可以定义多个目标
- 调用 make 命令时,得告诉它 我们希望构建的目标是什么,当没有指定目标时,make 将以默认目标作为生成目标(Makefile 中第一个目标为默认目标)
- 当 make 得倒目标后,先找到构建目标的对应规则,然后运行规则中的命令来达到构建目标的目的,一个规则可以包含多条命令
改动二:
all : test
@echo “hello world”
test
:
@echo “just for test”
- 目标:all 、test
- 先决条件:test
- 命令:@echo “hello world” 、@echo “just for test”
$ make
just for test
hello world
$ make test
just for test
从运行结果中:
- 当 make 不带参数时,构建 all 目标之前 test 目标也被构建了,all 目标后面的 test 告诉 make ,all 目标依赖于 test 目标,这个依赖目标被称为(all 目标的)先决条件
- 一个规则是由目标、先决条件及命令组成。目标和先决条件之间就是依赖关系(dependency),这种依赖关系指明:在构建目标之前,必须保证先决条件先满足。
参考文献:《专业嵌入式软件开发》李云·著