1.Makefile是幹嘛的呢?
這麼說吧:你有一個文件A包含了文件B, C, D,E,F,G,K,W,Q等多個文件,當B~Q中某一個文件改變或多個文件改變時。但A文件不知道啊,那你就需要重新編譯,鏈接你的文 件了。 但是這麼多的文件我們在linux下用gcc一行一行的敲,那多麻煩啊!而Makefile就是來解決這個麻煩的,當你寫好了Makefile文件時,使用make命令後就會直接幫你加載那些改變的文件。
2.源碼
用vi編輯main.c myTest1.c myTest.h myTest2.c myTest2.h 等五個文件。
代碼如下:
main.c
#include "myTest1.h"
#include "myTest2.h"
int main(int argc, char** argv)
{
myTest1_print("Hello");
myTest2_print("World");
}
myTest1.c
#include "myTest1.h"
void myTest1_print(char* str)
{
printf("This is myTest1 print %s\n", str);
}
myTest1.h
#ifndef _MYTEST_1_H
#define _MYTEST_1_H
void myTest1_print(char* str);
#endif
myTest2.c
#include "myTest2.h"
void myTest2_print(char* str)
{
printf("This is myTest2 print %s\n",str);
}
myTest2.h
#ifndef _MYTEST_2_H
#define _MYTEST_2_H
void myTest2_print(char* str);
#endif
Makefile
main:main.o myTest1.o myTest2.o
gcc -o main main.o myTest1.o myTest2.o
main.o: main.c myTest1.h myTest2.h
gcc -c main.c
myTest1.o: myTest1.c myTest1.h
gcc -c myTest1.c
myTest2.o: myTest2.c myTest2.h
gcc -c myTest2.c