1.自动化构建-make/makefile
1.1 背景
- 一个工程文件中的文件不计其数,其按类型、功能、模块放在若干目录中,makefile定义了一系列规则来指定哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于过呢更复杂的功能操作。
- makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make指令,整个工程完全自动编译
- make是一个指令,makefile是一个文件,两个搭配使用完成项目自动化构建
1.2基本使用
#include <stdio.h>
int main()
{printf("hello world\n");return 0;
}
Makefile文件
test:test.cgcc test.c -o test.PHONY:clean
clean:rm -f test
- 依赖关系:上面的文件test,依赖test.c
- 依赖方法:
gcc test.c -o test
就是对应的依赖方法 - 项目清理
1.工程时需要清理的
2.向clean这种没有被第一个目标文件直接或间接关联,那么他后面所对应的命令将不再被自动执行,不过我们可以显示要make执行即命令“make clean”来清理目标文件
3.但是一般我们这种clean的目标文件,我们将他设置为伪目标用.PHONY
修饰,伪目标的特性是:总是被执行的
1.3 make是如何工作的
test:test.ogcc test.o -o test
test.o:test.cgcc -c test.c -o test.o.PHONY:clean
clean:rm -f *.i *.s *.o test
- make会在当前目录下寻找名字为"Makefile"或“makefile”的文件。
- 如果找就会找文件中的第一个目标文件。
- 如果test文件不存在或是test所依赖的后面的test.o文件的修改时间要比这个test新,那么就会执行后面所定义的命令来生成test这个文件。
- 如果test依赖的test.o文件不存在,那么make会在当前文件寻找test.o的依赖性,如果找到则根据规则生成一个test.o文件。
- 当然.c文件.h存在于是make会生成test.o文件然后在用.o文件生成目标文件。
- 这就是make的依赖性,make会一层一层的去找文件的依赖关系。
- 在寻找过程中,如果出现错入那么make会直接退出,并且报错。而对于所定义的命令的错误或是编译不成功,make根本不理
- make只管文件的依赖性,即在找到依赖关系后,冒号后面的文件还是不在,那么make就不工作了
1.4 适度扩展语法
compling ... test.c to test.o
linking ... test.o to test.exe
[jfs@superg-alicloud test07]$ cat Makefile
BIN=test.exe # 定义变量
CC=gcc
#SRC=$(shell ls *.c) # 采用shell命令行式,获取当前所有.c文件名
SRC=$(wildcard *.c) # 或者使用wildcard函数,获取当前所有.c文件名
OBJ=$(SRC:.c=.o) # 将SRC的所有同名.c替换成为.o目标文件列表
LFLAGS=-o # 链接选项
FLAGS=-c # 编译选项
RM=rm -f # 引入命令$(BIN):$(OBJ)@$(CC) $(LFLAGS) $@ $^ # $@:代表目标文件名。 $^: 代表依赖文件列表@echo "linking ... $^ to $@"%.o:%.c # %.c 展开当前录下所有的.c。 %.o: 同时展开同名.o@$(CC) $(FLAGS) $< # %<: 对展开的依赖.c文件,一个一个的交给gcc。@echo "compling ... $< to $@" # @:不回显命令.PHONY:clean
clean:@$(RM) $(OBJ) $(BIN) # $(RM): 替换,用变量内容替换它.PHONY:test
test:@echo $(SRC)@echo $(OBJ)
2.Linux简单的系统程序-进度条
通过主函数传递参数模拟下载过程
myproc.h
#pragma once#include <stdio.h>void download(double total, double current, double speed);
myproc.c
#include "myproc.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);static int cnt=0;int num = (int)(current*100/total);int i = 0;for(; i < num; i++){buffer[i]=STYLE;}double rate = current*100/total;printf("[%-100s][%.1f][%c]\r",buffer,rate,lable[cnt%len]);cnt++;fflush(stdout);}
void process_v2(double total, double current, double speed)
{while(current <= total){FlushProcess(total, current);usleep(5000);current += speed;}
}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(50000);}printf("\n");
}void download(double total, double current, double speed)
{//process_v1();process_v2(total, current, speed);printf("\n download %.2fMB\n",current);
}
main.c
#include "myproc.h"double total = 1024.0;
double current = 0.0;
double speed = 1.0;
int main()
{download(total, current, speed);return 0;
}