Use of make/makefile

15802793:

make/makefile


make is a command, a command tool to explain the instructions in makefile, makefile is a file, the file in the current directory, the two are used together to complete the automatic construction of the project

make/makefile是Linux项目自动化构建工具,将多个源文件放在makefile中,通过make命令,通过makefile文件中的依赖关系,使得文件实现自动化编译,极大的提高了软件开发的效率。

Get acquainted with the workflow of makefile

下面通过一段代码进行编译演示makefile的工作流程

1. Understand the content format requirements of the makefile

insert image description here

2. make command

insert image description here

上述内容,我们通过make命令,通过makefile文件中的依赖关系实现了test可执行程序。

Dependencies and dependent methods

依赖关系为我们要实现的可执行文件是需要依赖于一些文件,通过依赖方法来实现的。只需要找到合适的依赖关系和依赖方法,我们就可以自动化编译

insert image description here

//1.依赖方法实际上就是实现依赖关系的语句,来得到可执行文件
test:test.c   //当前目录下进行查找发现拥有test.c文件
    gcc -c test test.c  //我们通过改gcc语句来得到test
//2.如果拥有多对依赖关系的时候,对于依赖关系的文件在该makefile所在目录下进行查找,如果没有,就查看makefile中是否有能得到该文件的依赖关系,然后一步一步的向下,直到查找到的依赖关系的文件是已存在的,可以从这一依赖关系,开始运行gcc语句,来得到对应文件来作为另一依赖关系的依赖文件
    
  1 test:test.o      			//test.o 文件不存在,所以向下找
  2     gcc -o test test.o
  3 test.o:test.s				//得到.o需要.s文件,所以还是没法运行对应的gcc语句(依赖方法)
  4     gcc -c test.s -o test.o
  5 test.s:test.i				//。。。
  6     gcc -S test.i -o test.s                                                       
  7 test.i:test.c				//最后找到了.c文件的依赖关系(.c是makefile所在目录下已有的文件所以是可以执行对应的gcc语句的)
  8     gcc -E test.c -o test.i
//所以从 第8行开始执行.c得到.i,现在有了.i 在由test.s:test.i关系通过gcc -S test.i -o test.s  语句来得到.s以此类推,最后得到test可执行文件

use of make

我们创建好makefile文件之后,我们可以通过make命令来运行该文件,但是我们发现的是make只能运行一次,情况如下:

insert image description here

//1.为什么会这样呢?难道不可以运行make创建新的test覆盖原来的test可执行文件
	对于make命令来讲,这是为了提高编译效率
//2.那么是如何判断是否需要更新该test文件的呢?
    我们先认识一个事情:先有源文件,再通过一定的依赖方法,得到可执行程序,所以一般来讲源文件的最近的修改时间比可执行文件要老(旧)
    所以当我们更改源文件之后,历史上曾经还有可执行文件,那么源文件的最近修改的时间,一定是 比可执行文件要新的。
    我们比较的是可执行程序的最近修改时间和源文件的最近修改时间
        如果.exe 新于 .c源文件 那么说明源文件是老的,即不需要重新编译
        如果.exe 旧于 .c源文件 那么说明.c源文件在实现.exe之后被更改了,所以需要重新编译
      	一般 .exe!=.c

对于上述内容总结,make会根据源文件和可执行程序的新旧,来判定是否需要重新执行依赖关系进行编译!!!

Check the file status command: stat file name

insert image description here

The content of the file is the file attribute, file = file content + file attribute

下面是对于三种时间的演示:

insert image description here

//对于上述的内容我们知道了
//1.Access是多次访问之后才会进行改变,是为了系统性能考虑
//2.Modify是文件内容改变之后才会改变,相应的一般大小变化之后Change也会改变,所以一般Modify==Change
//3.Change是文件属性变化才会改变(权限,大小等),一般只有Change会发生变化(Access可能变)

所以对于make判断源文件和目标文件的新旧问题,我们采用的是Change的时间(因为稳定,文件发生变化,就一定会改变时间)

Demo make for old and new issues with files
insert image description here

If you need to specify the specified dependencies every time: use .PHONY: pseudo-target

.PHONY:test 表示不管如何(test.c不更改也可以)都会执行该依赖关系

[External link image transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the image and upload it directly (img-2A3ZsIRU-1689503950289) (C:\Users\Hongyan\AppData\Roaming\Typora\typora-user-images\image-20230716175439430.png)]

Run the display:

insert image description here

Use of make clean

make clean 使得make的操作更加简便,想要删除指定可执行程序的时候,不用去rm,直接make clean即可

操作演示:
insert image description here

insert image description here

Summarize

  1. The default implementation of make is the first part (module) in the makefile

  2. make + target can achieve the specified target (part) in the makefile, such as make clean, make test, etc.

  3. The default file of make is makefile/Makefile, if you use other files to operate, use: make -f filename

  4. For the understanding of dependencies and dependent methods, and the fact that make cannot be executed continuously, it is because the target file is already up-to-date, and the information of the specified file can be viewed through stat filename. Change the information of the source file to compile again with make

  5. stat filename (filename), in the form of stat+filename, we can view three kinds of time other than the basic information of the file

They are:

  • Access: Indicates the time when the file was most recently accessed
  • Modify: Indicates the time when the content of the file was most recently modified
  • Change: Indicates the time when the file attributes were recently modified

For the performance of the linux system, Access needs to access the file multiple times to automatically modify the time.

Generally speaking, Modify changes, the file size changes, so Change changes, file attributes change, generally only Change changes, so we compare the Change time of the source file and the executable program whether to recompile the target file.

Guess you like

Origin blog.csdn.net/qq_63319459/article/details/131753485