主页 > 其他  > 

【make/Makefile】Linux下进度条的设计与实现

【make/Makefile】Linux下进度条的设计与实现
一、简单介绍make/Makefile

Makefile 和 makefile 不区分大小写,但是一定只能是 “makefile” !!!

make 是一个指令,makefile 是一个文件。

Makefile 格式形式:

使用 make 生成目标文件时,默认从上到下扫描 Makefile 文件,默认形成的是第一个目标文件——默认只生成一个。

二、进度条的设计与实现

进度条应与实际的业务结合,单独存在没有意义。因此,下面模拟了一个下载场景,结合下载场景完成进度条的设计。

【Makefile 文件】 processbar:test.o processbar.o gcc -o $@ $^ test.o:test.c gcc -c test.c processbar.o:processbar.c gcc -c processbar.c .PHONY:clean clean: rm -f processbar test.o processbar.o 【processbar.h】 1 #include <stdio.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <stdlib.h> 5 #include <time.h> 6 7 8 #define MAX 103 9 10 #define Head '>' 11 #define Body '=' 12 13 void process_fflush(double rate); 【processbar.c】 1 #include "processbar.h" 2 3 4 char* spit = "-\\|/"; 5 6 char buff[MAX] = {0}; 7 void process_fflush(double rate) 8 { 9 static int cnt = 0; 10 int n = strlen(spit); 11 if (rate < 0.5) buff[0] = Head; 12 13 printf("[%-100s][%.1f%%][%c]\r", buff, rate, spit[cnt%n]); 14 fflush(stdout); 15 buff[(int)rate] = Body; 16 if ((int)rate < 99) buff[(int)rate + 1] = Head; 17 18 cnt++; 19 20 if (rate >= 100.0) printf("\n"); 21 } 【test.c】 1 #include "processbar.h" 2 3 #define FILE_SIZE 1024*1024*1024 4 5 void download() 6 { 7 int total = FILE_SIZE; 8 srand(time(NULL)^1023); 9 while (total) 10 { 11 usleep(20000); 12 int one = rand()%(2 * 1024 * 1024); 13 total -= one; 14 if (total < 0) 15 total = 0; 16 17 int download = FILE_SIZE - total; 18 double rate = (download * 1.0 / (FILE_SIZE)) *100; 19 process_fflush(rate); 20 } 21 } 22 23 int main() 24 { 25 download(); 26 return 0; 27 }
标签:

【make/Makefile】Linux下进度条的设计与实现由讯客互联其他栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【make/Makefile】Linux下进度条的设计与实现