在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就可以執行了。