make 是一个解释Makefile 文件中指令的命令工具,其最基本的功能就是通过Makefile文件来描述源程序之间的相互关系并自动维护编译工作,它会告知系统以何种方式编译和链接程序。
Makefile写好之后,每次改变了某些源文件,只要执行make命令:#make
所有必要的重新编译将执行。Make程序利用makefile中的数据和每个文件的最后修改时间来确定那个文件需要更新,对于需要更新的文件,make程序执行makefile数据中定义的命令来更新。
假定一个项目中有以下一些文件。
源程序:Main.c、test1.c、test.c。
包含的头文件:head1.h、head2.h、head3.h。
由源程序和头文件编译生成的目标文件:Main.o、test1.o、test2.o。
由目标文件链接生成的可执行文件:test。
makefile文件例子
test:main.o test1.o test2.o
gcc -o test main.o test1.o test2.o
main.o:main.c head1.h head2.h
gcc -c main.c
test2.o:test2.c head2.h
gcc -c test2.c
test1.o:test1.c head1.h
gcc -c test1.c
install:
cp test /home/xwj/Desktop/makefile/tmp
clean:
rm -f *.o
main.c
#include <stdio.h>
#include "head2.h"
int main()
{
printf("This is main fun.\n");
outputHead2();
return 0;
}
test1.c
#include "head1.h"
void outputHead1()
{
printf("\nThis is head1.\n");
}
test2.c
#include "head2.h"
void outputHead2()
{
printf("\nThis is head2.\n");
outputHead1();
}
head1.c
#ifndef HEAD1_H
#define HEAD1_H
#include <stdio.h>
void outputHead1();
#endif
head2.c
#ifndef HEAD1_H
#define HEAD1_H
#include <stdio.h>
#include "head1.h"
void outputHead2();
#endif
输入命令make就会执行makefile文件
输入make install或者make clean就会执行在makefile写好的相应命令。