makefile主要方便一次性编译与执行
实例:有四个文件:test.h,test.c,display.c,Max.c,
/*************************/
test.h:
#include <stdio.h>
#include <dlfcn.h>
int Max(int a, int b);
int display();
/***********************/
test.c:
#include "test.h"
int main()
{
int ret = 0;
ret = Max(1, 10);
printf("Max is %d\n",ret);
ret = display();
return 0;
}
/***************************/
display.c:
#include "test.h"
int display()
{
printf("Hello world\n");
return 0;
}
/*******************************/
Max.c:
#include "test.h"
int Max(int a, int b)
{
return a > b ? a:b ;
}
创建Makefile文件:
Makefile:
make:test.o display.o Max.o
gcc test.o display.o Max.o -o make
test.o:test.h test.c
gcc -c test.c -o test.o
display.o:test.h display.o
gcc -c display.c -o display.o
Max.o:test.h Max.c
gcc -c Max.c -o Max.o
clean:
rm -rf *.o make
现在可以执行make了,执行完后,会多.o和make的文件,直接./make就可以运行,得到执行结果
输入make clean可以清除.o与make文件