Linux project automation build tool-make/Makefile and git three axes

1. Background knowledge about make/makefile

Whether you can write make/makefile or not reflects from the side whether a programmer has the ability to complete large-scale projects. The source files in a project are not counted, and they are placed in several directories according to type, function, and module. The makefile defines a series of rules to specify which files need to be compiled first, which files need to be compiled later, and which files need to be re-compiled. Compile, and even perform more complex functional operations. The benefit brought by makefile is - "automatic compilation". Once written, only one make command is needed, and the entire project is completely automatically compiled, which greatly improves the efficiency of software development.
make is a command tool that explains the instructions in the makefile. Generally speaking, most IDEs have this command, such as: make in Delphi, nmake in Visual C++, make in GNU under Linux. It can be seen that makefiles have become a compilation method in engineering.
make is a command, makefile is a file, and the two are used together to complete the automatic construction of the project.

2. Dependencies and dependent methods

When using make/makefile to automate build tools, there are two "dependencies", dependencies and dependent methods. What are dependencies and dependent methods? For example, it’s the end of the month and you want to ask your dad for living expenses. You get on the phone and tell your dad, “Dad, I’m your son. I don’t have any living expenses. Send me some money for living.” Then You hang up the phone, and the sentence you said "Dad, I am your son" shows the dependence relationship between you and your father, and "you send me money" is the method of dependence, just because you are your father's son , that is, you and you have a dependent relationship, and your father will give you money, that is, the dependent method; if there is no dependent relationship, then there is no way to generate the dependent method. It is impossible for you to ask your roommate's father for money, because you have no dependent relationship and cannot create a dependent method. Going back to the code, the dependency relationship is that the generation of a certain file needs to rely on one or more other files as the basis. This is the dependency relationship between these files. The dependency method is the method of generating the target file, that is, how to compile to generate the target file .

3. How to write make/makefile?

First, you need to create a makefile/Makefile in the current directory. According to the entire process of compiling code, compiling is divided into: preprocessing, compiling, assembling, and linking to form an executable file.
insert image description here

insert image description here
But it is too complicated to write like the above, and we generally generate executable files directly in one step.
insert image description here
Because the generation of a file may need to rely on multiple files, when there are many dependent files, it is very inconvenient for us to write, so a special symbol form is produced.
$@ represents the left side of the colon in the dependency relationship, and $^ represents the right side of the colon in the dependency relationship. It is written as follows:
insert image description here

4. The three times of the file (Access, Modify, Change)

Careful friends must have discovered the following problems:
insert image description here
It can be seen that after we have written the code, the first make is compiled normally, but if we make it again without modifying the code, the compiler will not let us Compiled, what is the reason?
Then we will come to understand the three times of Access, Modify and Change.
insert image description here
If you want to manually update these three times, you can use the touch command. The function of touch+file name is to create the file if the file does not exist. If the file already exists, the three times will be updated.
insert image description here

Five, the writing of countdown and progress bar code under Linux

5.1 Carriage return and line feed

We have all heard carriage return and line feed together. Do you think carriage return and line feed are one word? Actually not, carriage return and line feed are two independent words, carriage return is carriage return, line feed is line feed, they have nothing to do with each other; carriage return means returning to the very beginning of the line, and line feed is switching to the next line. Under Linux, \r represents a carriage return, and \n actually has both a carriage return and a line feed.

5.2 Buffer

insert image description here
Observing the above picture, you will find that the code of the main function obviously prints hello world first! ! ! , and then sleep for two seconds, but according to the picture, it sleeps for two seconds first, and then prints hello world! ! ! Yes, what is the reason? Can the code still be executed from the bottom up? Of course not, it is impossible to execute the following code first, and then execute the above code, so the printing function must be executed first, but the printed content is not seen on the display, indicating that the printed code must be stored in a certain location. Wait until sleep and then refresh it on the display, so where is the print? In fact, it is in the buffer zone.
The buffer is a section of memory maintained by C language, which can be used to temporarily store the printed content, and it will be refreshed to the display screen after the buffer is full or the program ends. Why is there a buffer for temporary storage? Wouldn't it be nice to print directly to the monitor? Of course, the buffer has its meaning, because the monitor is an external device of the computer, and the access speed is slow. Frequently printing content to the monitor is not conducive to the improvement of the overall efficiency of the operating system and will reduce the efficiency of the operating system. The existence of the buffer can greatly improve the efficiency of the operating system. Reduce the frequency of accessing peripherals.
So how can we immediately refresh the content to the display? You can use the fflush interface function to refresh the contents of the buffer to the display in real time.
insert image description here

As shown in the figure above, use the fflush interface to refresh stdout (standard output: monitor) after printing, you
can see the printed result first and then sleep.

5.3 Countdown code implementation

insert image description here
insert image description here

5.4 Implementation of progress bar code

makefile
insert image description here
processBar.h
insert image description here
processBar.c
insert image description here

test.c

insert image description here

operation result:
insert image description here

Six, git three tricks

6.1 What is git?

Git is a version controller with network functions and is an open source software. I don’t know if you have ever experienced such an experience. For example, when you wrote an experiment report and finished the first version of the experiment report, you were scolded by the teacher when you submitted it, saying that there were problems everywhere, and asked you to take it back and revise it; Then you took it back and revised it again, and got the second version of the experiment report; after submitting it, the teacher said that some problems were corrected, but other problems were exposed again, and asked you to take it back and revise it; you revised it again and got the second version Three versions of the experimental report; when handing it in, the teacher said that the more you revised it, the worse it got, and you took it back for revision, and you revised it again and brought it to the teacher. The teacher scolded you with a straight face, and then said, forget it. Yes, your second version of the lab report is a little better, please give me your second version of the lab report! At this time, when you returned to the dormitory, you looked at the experimental report of the fourth version, and you couldn’t remember what the second version was like, because you didn’t make a backup, so you couldn’t restore the second version. The lab report is handed over to the teacher. From this example, we can see that when we are modifying and may need to compare different versions, it is best to make traces of each change first, and find the old version one by one. And git is used to do this version control. Common version controllers include svn and so on.
Git can be used as both a server and a client; the client is that we downloaded the git software on our own computer, and then created a directory locally for version control management. This directory is called a local warehouse. Because local things will be lost, once they are lost, they will not be found, so git has a cloud server with a very large memory as the server side, and the server side can also do version control management, that is, you can put your local The content of the warehouse is pushed to the remote warehouse. Since then, you are not afraid of losing the content of your local warehouse and not being able to find the version you want. This is the core functionality of git.

6.2 The commercial version of the git website

Many Internet companies have made some commercial websites based on git, such as gitee, github and so on. It's like Internet companies have made some commercial operating systems (OS) based on the Linux kernel, such as Centos, Ubuntu and so on.

6.3 git multi-person collaboration

In addition to version control, git can also complete multi-person collaboration. If you are working on a project in a company, it is usually completed by multiple people working together, so the code written by one person is also needed by teammates. We can upload it to git, and then pull it down on our respective computers. This allows multiple people to collaborate to complete the work.

6.3 git clone

git clone is to clone the remote warehouse to the local, so that the local warehouse and the remote warehouse can be connected.
insert image description here
insert image description here
After execution, the remote warehouse is cloned locally:
insert image description here

After the clone is completed, enter the linux directory, and there will be a .git hidden directory file. This .git file is the local warehouse of the cloud server. There is also an identical .git at the remote end of gitee, and the local warehouse is as shown in the figure below:
insert image description here
After the remote warehouse and the local warehouse have established a connection, you can push the code to the warehouse through the git three-axes.

6.4 git three tricks – git add

git add + file name
This command is to add your file to the temporary storage area, the specific operation is:
insert image description here

6.5 git three tricks – git commit

git commit -m "log information"
Note: The -m option must be present, that is, the log information must be present. Without -m, it cannot be submitted to the local warehouse.

This command is to actually submit the file to the local warehouse, the specific operation is:
insert image description here

6.6 git three tricks – git push

git push
is to push the code of the local warehouse to the remote warehouse.
The specific operation is:
insert image description here
after completing git push, you can see the code we pushed in the past on gitee, as shown in the figure below: including log information, etc.
insert image description here
When using git for the first time, you need to configure the username and email address.
insert image description here

The above is what I want to share with you today, have you learned it? If this article is helpful to you, please be careful and pay attention to it. We will continue to update Linux-related knowledge in the future. See you in the next issue! ! ! ! !

Guess you like

Origin blog.csdn.net/weixin_70056514/article/details/131606209