makefile简单概述

首先写一个简单的makefile

hello: main.o func1.o func2.o
(TAB)gcc main.o func1.o func2.o -o hello
main.o : main.c
(TAB)gcc -c main.c
func1.o : func1.c
(TAB)gcc -c func1.c
func2.o : func2.c
(TAB)gcc -c func2.c
.PHONY : clean
clean:
(TAB)rm -f hello main.o func1.o func2.o

规则:用于说明如何生成一个或多个目标文件,格式如下

target :prerequisites
(TAB)command

目标 依赖 命令

main.o : main.c
(TAB)gcc -c main.c

1.*** 第一条目标是最终要生成的目标这里是hello应用程序
2.*** 伪目标:makefile中把那些没有任何依赖只有执行动作的目标称为伪目标。
.PHONY 将clean目标声明为伪目标

.PHONY : clean
clean:
(TAB)rm -f hello main.o func1.o func2.o

3.*** 可以使用变量

obj = main.o func1.o func2.o func3.o
hello :$(obj)
(TAB)gcc $(obj) -o hello

$^ 代表所有依赖文件
$@ 代表目标
$< 代表第一个依赖文件

hello: main.o func1.o func2.o
(TAB)gcc main.o func1.o func2.o -o hello
等价于
hello: main.o func1.o func2.o
(TAB)gcc $^ -o $@

4.*** 杂项
makefile中#为注释
@:取消回显,使用方法

hello :hello.c
(TAB)@gcc hello.c -o hello

5.*** make命令会去找makefile
也可用make -f 文件名