Makefile的基本编写与优化

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"},{"type":"color","attrs":{"color":"#808080","name":"user"}}],"text":"提示:本文中使用的操作系统环境为Ubuntu18.0/64位。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"},{"type":"color","attrs":{"color":"#808080","name":"user"}}],"text":"文章第一次发布于CSDN:"},{"type":"link","attrs":{"href":"https://blog.csdn.net/liuchengz_/article/details/108062139","title":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"https://blog.csdn.net/liuchengz_/article/details/108062139"}],"marks":[{"type":"italic"},{"type":"color","attrs":{"color":"#808080","name":"user"}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Linux系统下编译文件通常需要我们使用命令进行编译,而不像时在window系统下许多编译器可以一键将我们编写的代码编译完成,而当我们的源文件数量很多的时候,使用Makefile进行编译会很大程度上的提高我们的效率。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"一、Makefile是什么?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Makefile其实就是一个文件,它默认命名为Makefile(或者makefile),它包含了一组自动化构建工具,用来生成目标文件的指令。我的理解就是将我们平时编译需要用到的一步步指令写到了一个脚本文件里,使用时只需要执行make指令就能执行Makefile中的指令。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"二、Makefile的基本格式"}]},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"target ... : prerequisites ...\n\tcommand\n\t...\n\t..."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这是一个文件的依赖关系,也就是说,target 这一个或多个的目标文件依赖于 prerequisites 中的文件,其生成规则定义在 command 中。值得注意的是,在 Makefile 中的命令,必须要以[Tab]键开始。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"target"}]},{"type":"text","text":"代表目标文件,即你要生成的文件,可以是中间文件也可以是最终的可执行文件。target还可以是一个Label,即一些我们自义定的操作,例如常用的clean,用于删除指定的文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"prerequisites"}]},{"type":"text","text":"代表用于前面的"},{"type":"codeinline","content":[{"type":"text","text":"target"}]},{"type":"text","text":"所需要的文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"command"}]},{"type":"text","text":"代表make需要执行的命令,可以是任意的Shell指令。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"举个例:我制作了一个计算器,其有加减乘除四个功能,我将其分为了多个文件编写,分别为cal.h、main.c、mul.c、div.c、add.c、sub.c(举例而已,真是的情况应该不会有人这么做)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代码如下(示例):"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"calculator : main.o mul.o div.o add.o sub.o \n\tgcc main.o mul.o div.o add.o sub.o -o calculator\nmain.o : main.c cal.h\n\tgcc -c main.c\nmul.o : mmul.c cal.h\n\tgcc -c mul.c\ndiv.o : div.c cal.h\n\tgcc -c div.c\nadd.o : add.c cal.h\n\tgcc -c add.c\nsub.o : sub.c cal.h\n\tgcc -c sub.c\nclean : rm calculator main.o mul.o div.o add.o sub.o "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们将这个内容保存为Makefile(或者makefile)然后再该目录下输入命令"},{"type":"codeinline","content":[{"type":"text","text":"make"}]},{"type":"text","text":"就可以生成执行文件calculator了。如果要删除执行文件和所有的中间目标文件,只需要输入"},{"type":"codeinline","content":[{"type":"text","text":"make clean"}]},{"type":"text","text":"就可以了,"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"三、基本结构构成"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1.变量定义"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于在Makefile中重复出现的文件,如刚才举例中的:main.c、main.o等文件名,我看可以将其自定义成我们所命名的变量。在Makefile中变量一般都是字符串,有点像C语言中的宏,当Makefile被执行时,其中的变量都会被扩展到相应的引用位置上。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.显示规则"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"make 会把其要执行的命令行在命令执行前输出到屏幕上。当我们用“@”字符在命令行前,那么,这个命令将不被 make 显示出来。如果 make 执行时,带入 make 参数“-n”或“--just-print”,那么其只是显示命令,但不会执行命令,这个功能可以用来调试我们的Makefile,观察我们编写的Makefile的执行顺序。而 make 参数“-s”或“--slient”则是全面禁止命令的显示。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3.隐晦规则"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我们使用 Makefile 时,有一些我们会经常使用,而且使用频率非常高的东西,make其实是预先规定好的,其具有自动推导的功能,它可以自动推导文件以及文件依赖关系后面的命令,如.o文件由.c文件编译而来,它们之间的依赖关系是预先就设定好的,于是我们就没必要去在每一个.o文件后都写上类似的命令"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.文件指示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用于Makefile的嵌套执行,在大型的工程项目中,我们将不同的模块放在不同的文件夹,然后为每个文件夹编写相应的Makefile,有利于我们Makefile的维护。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"5.注释"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Makefile中只有行注释,和UNIX的Shell脚本一样,其注释是用“#”字符,类似于C/C++中的“//”一样。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"四、提高编写效率"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在刚才的例子中我们对6个文件进行编译就Makefile就已经写了13行,似乎并不比我们手动将.c文件编译成.o目标文件然后再链接它们高效多少。其实再实际的工程项目中源文件远不止此,那么面对大量的源文件,我们可以灵活运用其基本结构中来提高我们的Makefile编写效率。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1、预定义变量"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Makefile中有许多变量预先就定义好了,可供我们直接调用。常用的预定义变量:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"$* "}]},{"type":"text","text":"不包含扩展名的目标文件名称。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"$+"}]},{"type":"text","text":" 所有的依赖文件,以空格分开,并以出现的先后为序,可能包含重复的依赖文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"$< "}]},{"type":"text","text":"第一个依赖文件的名称。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"$?"}]},{"type":"text","text":" 所有的依赖文件,以空格分开,这些依赖文件的修改日期比目标的创建日期晚。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"$@"}]},{"type":"text","text":" 目标的完整名称。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"$^"}]},{"type":"text","text":" 所有的依赖文件,以空格分开,不包含重复的依赖文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"$%"}]},{"type":"text","text":" 如果目标是归档成员,则该变量表示目标的归档成员名称。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2、使用通配符"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Makefile的编写中我们也可以用到我们平时使用到的通配符。如:星号代表任意个任意字符,"},{"type":"codeinline","content":[{"type":"text","text":"*"}]},{"type":"text","text":".o代表文件夹中所有.o文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述示例中的Make file修改后代码示例(如下):"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"bash"},"content":[{"type":"text","text":"calcultor : main.o mul.o div.o add.o sub.o \n\tgcc *.o -o $@\n%.o : %.c\n\tgcc -c $< -o $@\nclean:\n\trm -f *.o calcultor"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"五、总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Makefile其实在我们日常的生活中编写代码使用到的并不多,只有在对量很大的代码进行编译时才能体现它的优势,但是,学会去写Make file对我对于语言的编译原理上的理解会有一些帮助,所以总结了些基本的知识,希望可以帮助到大家。"}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"参考资料"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - 《跟我一起写Makefile》 作者:陈皓"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" - "},{"type":"link","attrs":{"href":"https://en.wikipedia.org/wiki/Makefile","title":""},"content":[{"type":"text","text":"Makefile"}]},{"type":"text","text":"-wikipedia"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章