1.make/Makefile
1.什么是make和Makefile?
- 一个工程中的源文件不计其数,它们按范例、功能、模块分别放在多少个目录中,makefile 界说了一系列的规则来指定,哪些文件必要先编译,哪些文件必要后编译,哪些文件必要重新编译,甚至于进行更复杂的功能操作。
- makefile 带来的利益就是 “自动化编译”,一旦写好,只必要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。
- make 是一个解释 makefile 文件中指令的命令工具,一样平常来说,大多数的IDE都有这个命令,好比:Delphi的make,Visual C++的nmake,Linux下GNU的make。可见 makefile 成为了一种在工程方面的编译方法。
- make 是一条命令,makefile 是一个文件,两个搭配使用,完成项目自动化构建。
- 会不会写makefile,从一个侧面说明白一个人是否具备完成大型工程的能力,makefile 中的 m 巨细写都可以。
Makefile 文件中添加自动化清算命令
上面的 .PHONY: 是什么?
更加详细的过程,但不保举这么写
2.stat命令
标题来了:make 命令怎样知道形成可执行文件后,源文件被修改了? 答案:Modify 时间
- touch的盲区:后添加一个已经存在文件,会更改文件的3个时间
- PHONY作用:忽略 Modify 时间,所有源文件重新编译
3.Makefile单个文件的写法
更通用的写法:界说变量
4.Makefile多个文件的写法
Makefile有多个文件?
在工程中,我们一样平常会将所有的源文件先编译成目的文件,再将所有的目的文件与动态库进行动态链接,生成可执行程序。
- #循环创建5个文件的命令
- count=1; while [ $count -le 5 ]; do touch code${count}.c; let count++; done
复制代码
2.进度条
1.回车\r、换行\n
- 回车\r:光标回到该行的第一个位置。
- 换行\n:光标垂直向下到下一行,光标不会回到第一个位置。
C语言中的\n:本质是 \r+\n。
2.缓冲区
若想要将没有带 \n 的字符串立刻革新?fflush
3.进度条
1.倒计时程序
最终版本的倒计时程序
2.进度条程序
版本一:一次性展现进度条
版本二:边下载/上传,边更新进度条
Makefile 文件
- SRC=$(wildcard *.c)
- OBJ=$(SRC:.c=.o)
- BIN=processbar
- $(BIN):$(OBJ)
- gcc -o $@ $^
- %.o:%.c
- gcc -c $< -std=c99
-
- .PHONY:clean
- clean:
- rm -rf $(OBJ) $(BIN)
复制代码 process.h
- #pragma once
- #include<stdio.h>
- void process_v1();
- void FlushProcess(double total, double current);
复制代码 process.c
- #include"process.h"
- #include<string.h>
- #include<unistd.h>
- #define NUM 101
- #define STYLE '#'
- void FlushProcess(double total, double current)
- {
- char buffer[NUM];
- memset(buffer, 0, sizeof(buffer));
- const char* lable = "|/-\";
- int len = strlen(lable);
- //不需要循环,只需填充#
- int num = (int)(current * 100 / total);
- for (int i = 0; i < num; i++)
- {
- buffer[i] = STYLE;
- }
-
- static int cnt = 0;
- cnt %= len;
- double rate = current * 100 / total;
- printf("[%-100s][%.1f%][%c]\r", buffer, rate, lable[cnt]);
- cnt++;
- fflush(stdout);
- }
- void process_v1()
- {
- char buffer[NUM];
- memset(buffer, 0, sizeof(buffer));
- const char* lable = "|/-\";
- int len = strlen(lable);
- int cnt = 0;
- while (cnt <= 100)
- {
- printf("[%-100s][%d%][%c]\r", buffer, cnt, lable[cnt % len]);
- fflush(stdout);
- buffer[cnt] = STYLE;
- cnt++;
- usleep(500);
- }
- printf("\n");
- }
复制代码 main.c
- #include"process.h"
- //函数指针
- typedef void (*callback_t) (double total, double current);
- double total = 1024.0; //文件大小
- double speed = 1.0; //下载速度
- //回调函数
- void DownLoad(callback_t cb)
- {
- double current = 0;
- while (current <= total)
- {
- cb(total, current);
- //下载代码
- usleep(3000); //充当下载数据
- current += speed;
- }
- printf("\ndownload %.2lfMB Done\n", current);
- }
- void UpLoad(callback_t cb)
- {
- double current = 0;
- while (current <= total)
- {
- cb(total, current);
- //上传代码
- usleep(3000); //充当上传数据
- current += speed;
- }
- printf("\nupload %.2lfMB Done\n", current);
- }
- int main()
- {
- DownLoad(FlushProcess);
- UpLoad(FlushProcess);
-
- return 0;
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |