当前位置: 移动技术网 > 网络运营>服务器>Linux > 实例详解Linux下的Make命令

实例详解Linux下的Make命令

2019年04月26日  | 移动技术网网络运营  | 我要评论

前言

无论是在linux 还是在unix环境 中,make都是一个非常重要的编译命令。不管是自己进行项目开发还是安装应用软件,我们都经常要用到make或make install。利用make工具,我们可以将大型的开发项目分解成为多个更易于管理的模块,对于一个包括几百个源文件的应用程序,使用make和 makefile工具就可以简洁明快地理顺各个源文件之间纷繁复杂的相互关系。而且如此多的源文件,如果每次都要键入gcc命令进行编译的话,那对程序员 来说简直就是一场灾难。而make工具则可自动完成编译工作,并且可以只对程序员在上次编译后修改过的部分进行编译。因此,有效的利用make和 makefile工具可以大大提高项目开发的效率。

make 如何工作的

对于不知道背后机理的人来说,make 命令像命令行参数一样接收目标。这些目标通常存放在以 “makefile” 来命名的特殊文件中,同时文件也包含与目标相对应的操作。更多信息,阅读关于 makefiles 如何工作的系列文章。

当 make 命令第一次执行时,它扫描 makefile 找到目标以及其依赖。如果这些依赖自身也是目标,继续为这些依赖扫描 makefile 建立其依赖关系,然后编译它们。一旦主依赖编译之后,然后就编译主目标(这是通过 make 命令传入的)。

现在,假设你对某个源文件进行了修改,你再次执行 make 命令,它将只编译与该源文件相关的目标文件,因此,编译完最终的可执行文件节省了大量的时间。

make 命令实例

下面是本文所使用的测试环境:

os —— ubunut 13.04
shell —— bash 4.2.45
application —— gnu make 3.81

下面是工程的内容:

$ ls 
anothertest.c makefile test.c test.h

下面是 makefile 的内容:

all: 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。

$ make 
gcc -c -wall test.c 
gcc -c -wall anothertest.c 
gcc -wall test.o anothertest.o -o test

你能看到 make 命令第一次创建的依赖以及实际的目标。

如果你再次查看目录内容,里面多了一些 .o 文件和执行文件:

$ ls 
anothertest.c anothertest.o makefile test test.c test.h test.o

现在,假设你对 test.c 文件做了一些修改,重新使用 make 编译工程:

$ make 
gcc -c -wall test.c 
gcc -wall test.o anothertest.o -o test

你可以看到只有 test.o 重新编译了,然而另一个 test.o 没有重新编译。

现在清理所有的目标文件和可执行文件 test,你可以使用目标 clean:

$ make clean
rm -rf *.o test

$ ls
anothertest.c makefile test.c test.h

你可以看到所有的 .o 文件和执行文件 test 都被删除了。

2. 通过 -b 选项让所有目标总是重新建立

到目前为止,你可能注意到 make 命令不会编译那些自从上次编译之后就没有更改的文件,但是,如果你想覆盖 make 这种默认的行为,你可以使用 -b 选项。

下面是个例子:

$ 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 选项。

这是一个例子:

$ 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'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.c'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.cc'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.c'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.cpp'.
trying pattern rule with stem `makefile'.
--more--

这是很长的输出,你也看到我使用了 more 命令来一页一页显示输出。

4. 使用 -c 选项改变目录

你可以为 make 命令提供不同的目录路径,在寻找 makefile 之前会切换目录的。

这是一个目录,假设你就在当前目录下:

$ ls 
file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt

但是你想运行的 make 命令的 makefile 文件保存在 ../make-dir/ 目录下,你可以这样做:

$ make -c ../make-dir/ 
make: entering directory `/home/himanshu/practice/make-dir' 
make: nothing to be done for `all'. 
make: leaving directory `/home/himanshu/practice/make-dir

你能看到 make 命令首先切到特定的目录下,在那执行,然后再切换回来。

5. 通过 -f 选项将其它文件看作 makefile

如果你想将重命名 makefile 文件,比如取名为 my_makefile 或者其它的名字,我们想让 make 将它也当成 makefile,可以使用 -f 选项。

make -f my_makefile

通过这种方法,make 命令会选择扫描 my_makefile 来代替 makefile。

总结

以上就是关于linux下make命令的全部内容,文中通过示例介绍的还是很详细的,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网