0 Preface/Foreword
0.1 常用伪目标
++常见的伪目标++ :all、 clean、install、test、rebuild、dist
1 伪目标
1.1 定义伪目标的方法
通过关键词PHONY进行定义。
++定义伪目标的方法如下++:
.PHONY: target1 target 2 target 3 target n-1 target n
或者单独定义伪目标:
.PHONY: target1
.PHONY: target2
.PHONY: target3
bash
# define phony target
.PHONY: build clean
#.PHONY: clean
.PHONY: list
.PHONY: rebuild
rebuild: clean build
build:
make arithmetical_operations
clean:
echo "remove executable file arithmetical_operations"
rm arithmetical_operations
list:
ls -l
test: arithmetical_operations.c
echo "test a normal target"

1.2 伪目标个数有限制
一个makefile中的伪目标没有个数限制 ,可以是任意个数。
1.3 执行逻辑和顺序
++执行逻辑和顺序如下++:
- ++默认执行++ :只运行make 命令而不指定目标,Make会执行makefile中定义的第一个目标 。(通常习惯将all放在第一个目标)
- ++指定执行++ :运行命令make 的同时,带上目标,比如 make clean ,只会执行clean目标下的命令;运行 make build则只会执行build下的命令。
- ++依赖控制顺序++:伪目标直接可以通过依赖关系来控制执行顺序。
1.4 伪目标的作用
通过**.PHONY**声明伪目标的作用:
- 避免文件名冲突:当目标名字和当前路径下的文件名同名时,make会忽略同名文件,强制执行命令。
- 提高性能 :Make知道伪目标不对应实际文件,因此不会去检查文件系统的时间戳 或搜索隐含规则,从而加快执行速度。