linux dependencies and simple progress bar implementation

One: What is a dependency?

Simply put, the production of a file requires the existence of another file

The previous piece of code below

   main.o:main.c
       gcc -c main.c

The meaning of this code is

My purpose is to produce main.o files

The required raw material is the main.c file

The method used is gcc -c main.c

This kind of dependency method, we usually implement it in the Makefile file

We generally use this method to quickly generate and clear code

Take the above as an example, let's edit the content of main.c first

1 #include<stdio.h>
  2 int main()
  3 {
  4   printf("hello wolrd");                                                                                                                                                               
  5   return 0;                                                                                                               
  6 }                                                                                                                         
    

A very simple hello world code

Then create a Makefile and enter the following

  1 hello:main.c
  2   gcc -o hello main.c                                                                                                                                                                  
  3 .PHONY:clean                  
  4 clean:                        
  5     rm -f hello

Then enter the command

make

we produced this file

 Enter ./hello to view

 appeared

If it is clean, enter make clean

just delete

 2. What is .PHONY (note that there is a dot)

The computer judges whether the file needs to be updated according to the attribute

For example, taking our example just now

We have generated a hello file

Then execute the make command again

 will not execute

This is because the two instructions do not make any modifications to the file. This kind of meaningless waste of resources is not supported by the computer.

So we need to use .PHONY

As long as we use this name we can override this behavior

But in order to prevent repetition, we generally put this behavior on the clear instruction

 As above, it can be cleared repeatedly without affecting

3. Make a simple progress bar

directly on the code

  1 #include<string.h>
  2 #include<stdio.h>
  3 #include<unistd.h>
  4 #define NUM 101
  5 #define STYLE '='
  6 void process()
  7 {
  8   char bar[NUM];
  9   memset(bar,'\0',sizeof(bar));
 10   const char* lable="|\\-/";
 11   int cnt=0;
 12   while(cnt<=100)
 13   {
 14     printf("加载中:\033[46;35m[%-100s][%d%%]%c\033[0m\r",bar,cnt,lable[cnt%4]);                                                                                                        
 15     fflush(stdout);
 16     bar[cnt++]=STYLE;
 17     usleep(20000);
 18   }
 19   printf("\n");
 20 }
 21 int main()
 22 {
 23   process();
 24   return 0;
 25 }

Guess you like

Origin blog.csdn.net/qq_62718027/article/details/126909498