【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 }  
  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值