[Linux operating system] Automatically compile make and Makefile

insert image description here

1. Introduction to make/makefile

1. What is make, makefile?

makeis a tool/command for building C++ projects ; makefileit is a script file that contains compilation commands . Interpret the commands in the makefile through the make tool to compile our project.

2. Why is there a make/makefile?

Developed under the Linux environment, when there are few project source files, you can use gcc to compile directly; but when there are many project source files, gcc directly compiles complicated (for example, there are many commands, the order of file compilation is determined, etc.) and it is not easy for later projects Therefore, using make/makefile to achieve automatic compilation is beneficial to project development.

2. Makefile rules

1. Basic rules

target:prerequisites
 	command

Makefile file writing basic rules:

Just like making a good dish, you need the ingredients you depend on, and you have to rely on the chef's good cooking skills!

image-20230107180844759

  1. Target: target, the target file to be generated is often the intermediate file or final file of the program, such as test.i, test.s, test.o, test
  2. Dependency: prerequisites, which files the target file is generated from, often one or more
  3. Command: command, by executing this command to get the target file from the dependent file, you need to pay attention that there must be one before the [tab键]command, there can be multiple commands, but each command must be on a single line!

In the makefile, [tab键]it cannot be omitted, and it cannot be replaced by spaces. [tab key] is not equal to 4 spaces or 8 spaces. A tab key is actually 4 characters, but it represents 4 characters.

2. Give an example

The Makefile is as follows:

ps:

  • The makefile file name can also be called:Makefile
  • Comments in the makefile file use " #"
test:test.c          #依赖关系
  gcc test.c -o test #依赖方法  
.PHONY:clean    
clean:    
  rm -rf test 
  • What is this .PHOINY? I'll talk about it later

:wq! How do we use the makefile after exiting vim?

image-20230107182534247

3. False target

Before introducing false targets, let's talk about the concept of real targets:

  • Real target: The real target is the file name to be generated after the command is testexecuted

  • Pseudo-target: The actual file will not be generated after the command is executed. It is often used for auxiliary operations. .PHONY is the label of the pseudo-target. It cleanis a pseudo-target and will not generate a file actually named clean.

    Characteristics of pseudo-targets: pseudo-targets can always be executed [why later]

image-20230107183755900

4. Other rules

variable name meaning
$@ Target file, which can represent test
$^ All dependent files can represent test.c
$< first dependency file
test:test.c    
  @gcc $^ -o $@                                                                                                              
.PHONY:clean    
clean:    
  @rm -rf test

@: When executing make without @ on the command line, the executed command will be echoed to the terminal, and @ will not be echoed

image-20230107184851539

Why when the command line is executed, the command to execute the first set of dependencies and dependent methods is make, but the command to execute the second set of dependencies and dependent methods is make clean?

In fact, first we default to the first set of dependencies and dependent methods can also be fully written: make test

It's just that we stipulate that the first group can omit test and only write make

3. Three time issues for files - make program

1. When will the three times be updated

We know: file = file content + file attributes

You can view the status of the file by stat + file name:

[li@VM-8-5-centos 1-7]$ stat test.c
  File: ‘test.c’
  Size: 74        	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 924282      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1002/      li)   Gid: ( 1002/      li)
Access: 2023-01-07 18:48:21.189648157 +0800
Modify: 2023-01-07 18:48:17.476581743 +0800
Change: 2023-01-07 18:48:17.476581743 +0800
 Birth: -
time meaning
Access (file access time) It changes when reading the file, such as cat/less, but ls will not update the time when viewing the file
Modify (file modification time) It changes when the file content is edited, such as vim/touch
Change (attribute modification time) It changes when the file attribute is modified, such as mv/chmod, etc.
  • Because the file must be accessed to complete the modification of the file content, so Modify updates, Access must also update

  • Because the content of the file is modified, the file size must change, so the Modify update, Change must also update

  • But the change of Access/Change will not cause the other two times to change

image-20230107212156116

Review the file attributes:

The attributes of a Linux file or directory mainly include : the node, type, permission mode, number of links, user and user group to which the file or directory belongs, the time of the latest access or modification, etc. of the file or directory.

-rw-rw-r-- 1 li li   62 Jan  7 18:47 Makefile
-rwxrwxr-x 1 li li 8360 Jan  7 20:55 test
-rw-rw-r-- 1 li li   74 Jan  7 18:48 test.c

2. Two functions of touch

We know that the touch command can create a file, and another function is to update the three times of the existing file as the system time.

touch test//不带选项,atime,mtime,ctime都更新
touch test -a//atime更新
touch test -m//mtime更新
touch test -c//ctime更新

3. How does the make program know whether the dependent files are updated?

There must be test.c first and then the test file, which means that at the beginning, the modification time of test.c must be older than the modification time of test.

If the make program finds that the last modification time of test is actually older than the last modification time of test.c, it means that test.c must have been modified after the last modification of test, so when making, the dependent method can be executed Success, and vice versa!

image-20230107211837209

Here we can also explain why .PHONYthere is a feature: the modified by .PHONY can always be executed, it may be that after the modification of .PHONY, it is no longer necessary to judge whether to recompile by comparing the modification time of test and test.c!

Guess you like

Origin blog.csdn.net/qq_64428099/article/details/128593345