makefile學習1:初識makefile

大材小用一下,下面給一個源文件寫makefile,認識一下makefile的依賴性。

源代碼:

#include "std.h"

int main(int argv,char **argc)
{
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int i = 0;
	for(;i < 10;i++)
	{
		printf("%d\n",arr[i]);
	}
	return 0;
}

頭文件:(std.h)

#ifndef _STD_H
#define _STD_H

#include <stdio.h>

#endif

makefile文件:

test:test.c std.h
	gcc test.c -o test

.PHONY:clean
clean:
	rm test 

執行:

gyz@debian:~/mc$ make
gcc test.c -o test
gyz@debian:~/mc$ ./test 
1
2
3
4
5
6
7
8
9
10
gyz@debian:~/mc$ make clean
rm test

可以看到最終的可執行文件是依賴於:後面的.c文件和.h文件的。

.PHONY是爲了說明clean是個僞命令。

如果把makefile寫成通用版本的,則變成:

test:test.o
	gcc -o test test.o
test.o:test.c std.h
	gcc -c test.c

.PHONY:clean
clean:
	rm test test.o

參考:http://www.cnblogs.com/liangxiaxu/archive/2012/07/31/2617384.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章