linux下如何使用make命令(linuxmakedir)

linux下如何使用make命令(linuxmakedir)

浏览次数:
信息来源: 用户投稿
更新日期: 2026-01-04
文章简介

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

2025阿里云双十一服务器活动

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

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

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

make命令实例下面是本文所使用的测试环境:

application——gnumake3.81

下面是工程的内容:

anothertest.cmakefiletest.ctest.h

下面是makefile的内容:

gcc-walltest.oanothertest.o-otest

anothertest.o:anothertest.c

rm-rf*.otest

现在我们来看linux下一些make命令应用的实例:

1.一个简单的例子

为了编译整个工程,你可以简单的使用make或者在make命令后带上目标all。

gcc-walltest.oanothertest.o-otest

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

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

anothertest.canothertest.omakefiletesttest.ctest.htest.o

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

gcc-walltest.oanothertest.o-otest

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

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

anothertest.cmakefiletest.ctest.h

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

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

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

make:nothingtobedonefor`all'.

gcc-walltest.oanothertest.o-otest

你可以看到尽管make命令不会编译任何文件,然而make-b会强制编译所有的目标文件以及最终的执行文件。

3.使用-d选项打印调试信息

如果你想知道make执行时实际做了什么,使用-d选项。

linux下如何使用make命令,linuxmakedir

thisisfreesoftware;seethesourceforcopyingconditions.

thereisnowarranty;notevenformerchantabilityorfitnessfora

thisprogrambuiltforx86_64-pc-linux-gnu

readingmakefile`makefile'…

consideringtargetfile`makefile'.

lookingforanimplicitrulefor`makefile'.

tryingpatternrulewithstem`makefile'.

tryingimplicitprerequisite`makefile.o'.

tryingpatternrulewithstem`makefile'.

tryingimplicitprerequisite`makefile.c'.

tryingpatternrulewithstem`makefile'.

tryingimplicitprerequisite`makefile.cc'.

tryingpatternrulewithstem`makefile'.

tryingimplicitprerequisite`makefile.c'.

tryingpatternrulewithstem`makefile'.

tryingimplicitprerequisite`makefile.cpp'.

tryingpatternrulewithstem`makefile'.

--more--

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

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

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

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

filefile2frndfrnd1.cpplog1.txtlog3.txtlog5.txt

file1filenamewithspacesfrnd1frnd.cpplog2.txtlog4.txt

$make-chttps://www.fruan.com/make-dir/

make:enteringdirectory`/home/himanshu/practice/make-dir'

make:nothingtobedonefor`all'.

make:leavingdirectory`/home/himanshu/practice/make-dir

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

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

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

make-fmy_makefile

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

以上就是linux下如何使用make命令的详细内容,更多请关注主机测评网其它相关文章!

标签:
什么是光纤分布式数据接口FDDI(光纤分布数据接口fddi采用)
« 上一篇
返回列表
下一篇 »

如本文对您有帮助,就请抽根烟吧!