Make的使用
在需要一次性执行多条命令时, 可以使用Make工具简化命令。
基础使用规则
在项目根目录新建一个Makefile文件, 内容的规则如下:
target : dependencies
action commands2
具体的例子:
在根目录下有一个C源代码和一个Makefile文件, 文件结构如下:
.
├── main.c
└── Makefile2
3
其中C源代码内容如下:
#include <stdio.h>
int main() {
printf("hello, make\n");
return 0;
}2
3
4
5
Makefile文件内容如下:
hello : main.c
gcc main.c -o hello2
WARNING
注意在Makefile文件第二行缩进必须用tab进行缩进,不能用空格代替,否则会报错。 使用空格的报错信息如下:Makefile:2: *** missing separator. Stop.。
在终端运行 make 命令则等价于运行 gcc main.c -o hello
arwell@arwell$ make
gcc main.c -o hello
arwell@arwell$ ls
hello main.c Makefile2
3
4
多个生成目标文件
将Makefile文件改为如下内容:
hello : main.c
gcc main.c -o hello
hello2 : main.c
gcc main.c -o hello22
3
4
5
在终端运行 make + 目标文件名即可执行对应的命令。
arwell@arwell$ ls
main.c Makefile
arwell@arwell$ make hello
gcc main.c -o hello
arwell@arwell$ ls
hello main.c Makefile
arwell@arwell$ make hello2
gcc main.c -o hello2
arwell@arwell$ ls
hello hello2 main.c Makefile2
3
4
5
6
7
8
9
10
伪目标(.PHONY)
有一些不生成目标文件的命令, 比如打包、清理多余文件等, 可以使用伪目标。
clean
例如把Makefile文件内容改为如下内容, 可以实现清理文件的作用。
hello : main.c
gcc main.c -o hello
hello2 : main.c
gcc main.c -o hello2
clean :
rm -f hello hello22
3
4
5
6
7
8
在终端中运行 make clean即可。
arwell@arwell$ ls
hello hello2 main.c Makefile
arwell@arwell$ make clean
rm -f hello hello2
arwell@arwell$ ls
main.c Makefile2
3
4
5
6
all
把 Makefile 文件内容改为如下内容, 可以实现一条命令生成多个目标文件。
all : hello hello2
echo "all done."
hello : main.c
gcc main.c -o hello
hello2 : main.c
gcc main.c -o hello2
clean :
rm -f hello hello22
3
4
5
6
7
8
9
10
11
在终端运行 make all
arwell@arwell$ ls
main.c Makefile
arwell@arwell$ make all
gcc main.c -o hello
gcc main.c -o hello2
echo "all done."
all done.
arwell@arwell$ ls
hello hello2 main.c Makefile2
3
4
5
6
7
8
9
TIP
在定义了伪目标all之后,直接运行make跟make all是等效的。
TIP
在make命令后加一个-s参数可以不在终端显示执行的命令; 在make命令后加一个-n参数可以只显示要执行的命令但是不执行。
TIP
如果在Makefile某条命令前加一个@可以在执行时不打印这一条命令。 例如:
all : hello hello2
@echo "all done."
hello : main.c
gcc main.c -o hello
hello2 : main.c
gcc main.c -o hello2
clean :
rm -f hello hello22
3
4
5
6
7
8
9
10
11
TIP
如果工作目录里有命名为clean、all等的文件, 执行make clean/all命令无法正常工作, 在Makefile第一行声明伪目标即可。
.PHONY: clean all
all : hello hello2
echo "all done."
hello : main.c
gcc main.c -o hello
hello2 : main.c
gcc main.c -o hello2
clean :
rm -f hello hello22
3
4
5
6
7
8
9
10
11
12
13
变量
定义变量和使用变量的方式如下, 可以用来添加编译参数。
.PHONY: clean all
Cflags = -Wall -g -O2
all : hello hello2
@echo "all done."
hello : main.c
gcc $(Cflags) main.c -o hello
hello2 : main.c
gcc $(Cflags) main.c -o hello2
clean :
rm -f hello hello22
3
4
5
6
7
8
9
10
11
12
13
14
自动变量
Makefile有自动变量, 比如$@表示目标文件, $<表示第一个依赖文件, $^表示所有的依赖文件。
于是上面的Makefile文件内容可以简化:
.PHONY: clean all
Cflags = -Wall -g -O2
all : hello hello2
@echo "all done."
hello : main.c
gcc $(Cflags) $< -o $@
hello2 : main.c
gcc main.c -o hello2
clean :
rm -f hello hello22
3
4
5
6
7
8
9
10
11
12
13
14