【Linux】make/Makefile & progress bar applet

Table of contents

1. Understanding make/makefile

2. Example code

1. Dependency

2. Principle

3. Project cleanup

4. Test explanation

3. Linux’s first small program - progress bar

game.h

game.c

test.c

Detailed explanation of the procedure


 

1. Understanding make/makefile

Whether or not you can write makefiles shows from one side whether a person has the ability to complete large-scale projects.

The source files in a project are not counted. 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 recompiled. Compile, and even perform more complex functional operations

The benefit of makefile is "automated compilation". Once it is 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 interprets the instructions in the makefile. Generally speaking, most IDEs have this command, such as: Delphi's make, Visual C++'s nmake, and GNU's make under Linux. It can be seen that makefile has become a compilation method in engineering.

Make is a command and makefile is a file. They are used together to complete the automated construction of the project.

2. Example code

Generally, we form an executable program like this:

Today we use make/makefile to implement;

The name of the created file must be the first letter of makefile. It can be uppercase or lowercase;

Directly enter the command make like this and the executable file will be automatically formed, which is very convenient;

1. Dependency

The above file mybin, which depends on game.o

game.o , which depends on game.s

game.s , which depends on game.i

game.i , which depends on game.c

2. Principle

How make works, in the default way, that is, we only enter the make command. So

1. make will look for a file named "Makefile" or "makefile" in the current directory.

2,If found, it will find the first target file (target) in the file. In the above example, it will find "mybin” this file, and use this file as the final target file.

3,ifmybinfile does not exist, or game.o file that >mybin depends on is longer than mybinThis file is new (you can use touch to test), then it will execute what is defined later command to generate the file mybin.

4,if ifhello depends ongame The .o file does not exist, then make will find the target in the current file as The dependency of the game.o file. If found, the game.o file will be generated according to that rule. . (This is a bit like a stack process) The game.o file it depends on does not exist, then makemake a> will find the dependency of the target file game.o in the current file. If it is found, it will be generated according to that rule. game.o file. (This is a bit like a stack process)

5,Of course, you're C sentence item and H sentence item is existential, you aremake Association generationgame.o text item, reuse afterward game.o text item Statement make's final appointment 务、也执行文品mybincompleted.

6, This is the dependency of the entire make, make will look for file dependencies layer by layer until the first target file is finally compiled.

7,During the search process, if an error occurs, such as the last dependent file cannot be found, thenmake will not work at all. reason. make will exit directly and report an error. However, for errors in the defined commands or failed compilation,

8, make only cares about file dependencies, that is, if the file after the colon is still not there after I find the dependencies, then I'm sorry, I won't work.

3. Project cleanup

The project needs to be cleaned up

Like clean, if it is not directly or indirectly associated with the first target file, then the commands defined after it will not be automatically executed. However, we can show that make is executed. That is, the command - "make clean" is used to clear all target files for recompiling.

But generally for our clean target files, we set it as a pseudo target and modify it with .PHONY. The characteristic of the pseudo target is that it is always executed.

You can declare our mybin target file as a pseudo target and test it.

4. Test explanation

The above are all principles for the grassroots level. There are a lot of words suitable for in-depth study. Now let’s make it simple;

mybin: game.c is a dependency, mybin is the executable file formed by game.c, but how to implement it specifically depends on the method, gcc game.c -o mybin is the dependency method;

And gcc game.c -o mybin can also be written as gcc -o $@ $^;

$@ means mybin, $^ means game.c

It has the same effect and is more convenient. It is recommended to use the latter;

.PHONY means that the file is modified into a pseudo file. The characteristic of the pseudo file is that it is always executed;

It is best to put clean below, so that the program is always executed and cleaned;

When we always execute make, the latter will be invalid because the modification time of mybin is slower than the modification time of game.c;

The red line drawn above is the modification time. Mybin is slower than game.c, so make will not be executed;

Let’s modify the game.c file and take a look;

Just finished the modification, the time of game.c is slower than that of mybin;

So you can run make. When you open it again, the modification time of the mybin file will be longer than that of game.c, and make will not be executed;

We can also use touch to refresh the modification time of the file;

If we declare our mybin target file as a pseudo target, test it.

In this way, make will be executed every time, but it is not recommended to use it because it is correct in itself. Don’t execute it without modification. There is nothing wrong with it. Now our file is small and it doesn’t matter if it is executed every time. If our file is very large time, it will be very time consuming if executed every time;

clean is used to clean files, if used it is make clean;

Clean files directly with one click;

Oh, by the way, there is another one we use: make. Why does it execute mybin instead of clean?

The command make is executed from top to bottom;

Replace them in order;

Then when we execute make, we execute clean. When we execute make mybin, we form the executable file mybin. This is from top to bottom, and the order does not affect;

3. Linux’s first small program - progress bar

The brothers began to work hard;

First prepare the documents you need at the beginning;

game.h

#include<stdio.h>
#include<time.h>
#include<unistd.h>

void game(double rate);
void download();

game.c

#include"game.h"
#define MAX 1024*1024*1024

char* buff = "|/-\\";
int i = 0;
char arr[102] = { 0 };
void game(double rate)
{
	if (rate <= 1.0)
	{
		arr[0] = '=';
	}
	printf("[%-100s][%.1lf%%][%c]\r", arr, rate, buff[i % 4]);
	fflush(stdout);
	arr[(int)rate] = '=';
	if (rate< 99.0)
	{
		arr[(int)rate+1] = '>';
	}
	i++;
}

void download()
{
	srand(time(NULL)^1023);
	int max = MAX;
	int cnt = 0;
	double rate = 0;
	while (rate<100.0)
	{
		cnt+= rand() % (1024*1024);
		rate = ((cnt*1.0)/max)* 100;
		if (rate > 100)
		{
			rate = 100;
		}
		game(rate);
		usleep(50000);
	}
}

test.c

#include"game.h"

int main()
{
	download();
	return 0;
}

Detailed explanation of the procedure

This program makes a memory progress bar, let MAX be the memory size that needs to be downloaded, then cnt is the downloaded part, rate is the downloaded percentage, and then pass it to the game function to run the progress bar, the game function, according to rete The size is output as a percentage, as is the equal sign, and there is also a flipped array to determine whether the program is stuck. Then add > after the equal sign to embellish the output until 100%, and exit the program;

Guess you like

Origin blog.csdn.net/m0_71676870/article/details/134463213