在linux下我们都知道可以利用命令gcc hello.c -o hello 命令来变异c语言程序。其中gcc hello.c -o hello中 hello是给这个编译后生成的可执行文件取个别名 再利用./hello命令运行c程序。
可是如果c程序比较多呢?每个都这样编译太麻烦了,聪明的程序员想出了一个很好的工具,那就是make,利用make来实现同时编译。
首先,假设有3个文件
1.greeting.h
其中内容是
#ifndef _GREETING_H
#define _GREETING_H
void greeting(char *a);
#endif
2.greeting.c //具体实现greeting.h的函数
#include<stdio.h>
#include"greeting.h"
void greeting(char *a)
{
printf("Hello %s\n",a);
}
3.test.c //主程序,用来调用greeting(char *a)函数的
#include<stdio.h>
#include"greeting.h"
int main()
{
char a[20] = "zzl oyxt";
greeting(a);
}
接下来就是来写我们的Makefile了
vi Makefile进行文件编辑
test : greeting.o test.o
gcc greeting.o test.o -o test
greeting.o: greeting.h greeting.c
gcc -c greeting.c
test.o:test.c greeting.h
gcc -c test.c
编辑完成保存之后,输入make命令,会生成一个可执行文件 test
那么执行./test就可以执行了。