Linux 下 Make 命令实例详解


Linux 下 Make 命令实例详解

文章插图
Linux 下 Make 命令实例详解
Linux 下 make 命令是系统管理员和程序员用的最频繁的命令之一 。管理员用它通过命令行来编译和安装很多开源的工具 , 程序员用它来管理他们大型复杂的项目编译问题 。本文我们将用一些实例来讨论 make 命令背后的工作机制 。
Make 如何工作的
对于不知道背后机理的人来说,make 命令像命令行参数一样接收目标 。这些目标通常存放在以 “Makefile” 来命名的特殊文件中,同时文件也包含与目标相对应的操作 。更多信息,阅读关于 Makefiles 如何工作的系列文章 。服务器教程
当 make 命令第一次执行时 , 它扫描 Makefile 找到目标以及其依赖 。如果这些依赖自身也是目标,继续为这些依赖扫描 Makefile 建立其依赖关系 , 然后编译它们 。一旦主依赖编译之后,然后就编译主目标(这是通过 make 命令传入的) 。
现在,假设你对某个源文件进行了修改,你再次执行 make 命令,它将只编译与该源文件相关的目标文件,因此,编译完最终的可执行文件节省了大量的时间 。
Make 命令实例
下面是本文所使用的测试环境:
?
1
2
3OS —— Ubunut 13.04
Shell —— Bash 4.2.45
Application —— GNU Make 3.81
下面是工程的内容:
?
1
2$ ls
anotherTest.c Makefile test.c test.h
下面是 Makefile 的内容:
?
1
2
3
4
5
6
7
8
9
10
11
12
13all: test
test: test.o anotherTest.o
gcc -Wall test.o anotherTest.o -o test
test.o: test.c
gcc -c -Wall test.c
anotherTest.o: anotherTest.c
gcc -c -Wall anotherTest.c
clean:
rm -rf *.o test
现在我们来看 Linux 下一些 make 命令应用的实例:
1. 一个简单的例子
为了编译整个工程,你可以简单的使用 make 或者在 make 命令后带上目标 all 。
?
1
2
3
4$ make
gcc -c -Wall test.c
gcc -c -Wall anotherTest.c
gcc -Wall test.o anotherTest.o -o test
你能看到 make 命令第一次创建的依赖以及实际的目标 。
如果你再次查看目录内容,里面多了一些 .o 文件和执行文件:
?
1
2$ ls
anotherTest.c anotherTest.o Makefile test test.c test.h test.o
现在 , 假设你对 test.c 文件做了一些修改 , 重新使用 make 编译工程:
?
1
2
3$ make
gcc -c -Wall test.c
gcc -Wall test.o anotherTest.o -o test
你可以看到只有 test.o 重新编译了 , 然而另一个 Test.o 没有重新编译 。
现在清理所有的目标文件和可执行文件 test,你可以使用目标 clean:
?
1
2
3
4
5$ make clean
rm -rf *.o test
$ ls
anotherTest.c Makefile test.c test.h
你可以看到所有的 .o 文件和执行文件 test 都被删除了 。
2. 通过 -B 选项让所有目标总是重新建立
到目前为止,你可能注意到 make 命令不会编译那些自从上次编译之后就没有更改的文件,但是 , 如果你想覆盖 make 这种默认的行为 , 你可以使用 -B 选项 。
下面是个例子:
?
1
2
3
4
5
6
7$ make
make: Nothing to be done for `all’.
$ make -B
gcc -c -Wall test.c
gcc -c -Wall anotherTest.c
gcc -Wall test.o anotherTest.o -o test
你可以看到尽管 make 命令不会编译任何文件,然而 make -B 会强制编译所有的目标文件以及最终的执行文件 。
3. 使用 -d 选项打印调试信息
如果你想知道 make 执行时实际做了什么,使用 -d 选项 。
这是一个例子:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26$ make -d | more
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
Reading makefiles…
Reading makefile `Makefile’…
Updating makefiles….
Considering target file `Makefile’.
Looking for an implicit rule for `Makefile’.
Trying pattern rule with stem `Makefile’.
Trying implicit prerequisite `Makefile.o’.

推荐阅读