Linux项目自动化构建工具-make/Makefile 其实总的来说make是一条命令而Makefile是一个文件,在讲多文件的make文件编写前我们先将单文件的makefile文件怎么写如下程序所示
1 Mytest:test.o
2 gcc test.o -o Mytest
3 test.o:test.s
4 gcc -c test.s -o test.o
5 test.s:test.i
6 gcc -S test.i -o test.s
7 test.i:test.c
8 gcc -E test.c -o test.i
9
10
11 .PHONY:clean
12 clean:
13 rm -f test.i test.o test.s Mytest
其中我们写Makefile文件是要达到某种目的,比如要编译所写的程序而编译程序,不外乎是要有 依赖关系和依赖方法如下图所示,同时我们还要能够清除我们的程序这里使用伪命令.PHONY:clean 而上述中的依赖关系是一个test.i 到test.o 文件的全过程这里不懂可已看c语言生成可执行程序的全过程的博客,我们熟练后可以写成如下程序`
1 Mytest:test.c
2 gcc test.c -o Mytest
3
4 .PHONY:clean
5 clean:
6 rm -f test.o Mytest
总的要写成我们的Makefile文件步骤如下
1、创建一个Makefile文件 [sdk@VM-16-14-centos test]$ touch Makefile
2、打开我们创建的Makefile文件[sdk@VM-16-14-centos test]$ vim Makefile
3、写Makefile的依赖关系和依赖方法
Mytest:test.c
gcc test.c -o Mytest
.PHONY:clean
clean:
rm -f test.i test.o test.s Mytest
4、完成后make一下
[sdk@VM-16-14-centos test]$ make
gcc test.c -o Mytest
5、然后执行我们生成的可执行程序./mytest
[sdk@VM-16-14-centos test]$ ls
Makefile Mytest test.c
[sdk@VM-16-14-centos test]$ ./Mytest
hello world
hello world
hello world
6、删除我们生成的文件和可执行程序文件
[sdk@VM-16-14-centos test]$ make clean
rm -f test.i test.o test.s Mytest
[sdk@VM-16-14-centos test]$ ls
Makefile test.c
以上就是生成我们自己的Makefile文件的全过程了
接下来是一个多文件的Makefile的编写就不在详细赘述。
1、Makefile 文件内容:
1 mytest:main.o test.o
2 gcc -o mytest main.o test.o
3 main.o:main.c
4 gcc -c main.c -o main.o
5 test.o:test.c
6 gcc -c test.c -o test.o
7
8 .PHONY:clean
9 clean:
10 rm -f *.o mytest
2、进度条的程序
//[sdk@VM-16-14-centos lesson3]$ cat test.h
#ifndef _TEST_H
#define _TEST_H
#include<stdio.h>
#include<unistd.h>
#include<string.h>
//extern void show();
extern void ProcBar();
#endif
//[sdk@VM-16-14-centos lesson3]$ cat test.c
#include"test.h"
extern void ProcBar()
{
int i=0;
char bar[102];
memset(bar,'\0',sizeof(bar));
const char *lable="|/-\\";
while(i<=100)
{
printf("[\033[0m\033[1;31m%-100s\033[0m][%d%%][%c]\r",bar, i, lable[i%4]);
fflush(stdout);
bar[i]='#';
usleep(30000);
i++;
}
printf("\n");
}
//[sdk@VM-16-14-centos lesson3]$ cat main.c
#include"test.h"
int main()
{
ProcBar();
return 0;
}
程序效果