linux--make/进度条
发布日期:2025-04-07 03:05:15 浏览次数:4 分类:精选文章

本文共 2089 字,大约阅读时间需要 6 分钟。

Linux中的make与一个进度条的例子

1. makefile是什么?

makefile 是用于自动化编译和构建项目的特殊文件,包含了一系列编译规则和依赖关系。它定义了哪些源文件需要编译,哪些目标文件需要连接,甚至可以指定清理命令。通过编写makefile,开发者可以只用一条make命令就能完成整个项目的构建。

2. make命令的作用

make 是一个命令行工具,它负责解读和执行其 družūp,⼀般被各种开发环境(如Delphi、Visual C++、Linux的make)所包含。make 的工作流程:

  • 找到对应的Makefile。
  • 找到Makefile中第⼀个目标(⼆)
  • 为每个目标生成对应的命令。
  • 执行所有必要的编译和连接步骤。
  • 3. 依赖关系与清理

    在Makefile中,依赖关系通常用箭头表示:目标文件 Depends On 源文件。

    • 清理命令:清理目标文件需要明确指定,否则make clean 可能不会自动清理。如果目标需要伪处理(always execute),可以用.PHONY修饰。

    4. 示例:进度条

    process.c

    #include 
    #include
    #define NUM 101#define STYLE '='void process_v1() { char buffer[NUM]; memset(buffer, 0, sizeof(buffer)); const char *label = "|/-\\"; int len = strlen(label); int current = 0; while (current <= 100) { printf("[%-100s][%d%%][%c]\r", buffer, current, label[current % len]); fflush(stdout); buffer[current] = Style; current++; usleep(50000); } printf("\n");}

    process.h

    #pragma once#include 
    void process_v1();void FlushProcess(double total, double current);

    main.c

    #include "process.h"#include 
    #include
    double total = 1024.0;double speed = 1.0;void FlushProcess(double total, double current) { char buffer[NUM]; memset(buffer, 0, sizeof(buffer)); const char *label = "|/-\\"; int len = strlen(label); static int cnt = 0; double current_percent = (current * 100) / total; int num = (int)(current_percent); for (int i = 0; i < num; ++i) { buffer[i] = STYLE; } cnt %= len; printf("[%-100s][%.1f%%][%c]\r", buffer, current_percent * 100, label[cnt]); cnt++; fflush(stdout);}int main() { DownLoad(); DownLoad(); DownLoad(); DownLoad(); DownLoad(); DownLoad(); DownLoad(); DownLoad(); return 0;}

    Makefile

    SRCS = *.cOBJS = $(SRCS:.c=.o)BIN = processbar$(BIN):  $(CC) -o $@ $^%.o : %.c  $(CC) -c $^.PHONY: cleanclean:  rm -f $(OBJS) $(BIN)

    5. 优化与注意事项

    • 代码清晰:确保每部分代码有注释,便于阅读和维护。
    • 进度条:默认情况下,printf 的输出不可见,需使用flush 刷新。
    • 下载模拟usleep 用于模拟下载延迟,current 累加来表示传输进度。

    通过make命令可以自动校验依赖关系,确保所有文件存在后再执行构建步骤。如果发现缺失文件,make 会立即退出并提示错误。

    上一篇:Linux--NFS和DHCP服务器
    下一篇:linux--Linux 各目录及每个目录的详细介绍

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年05月10日 23时56分35秒