包含3個檔案夾,和一個檔案Makefile
目錄組織圖如下:
Makefile
inc/hello.h
main/main.c
src/hello.c
Makefile檔案在外面,這樣產生的.o和可執行檔都在外面,clean之後會很乾淨,結構清晰
檔案內容如下:
Makefile(之所以用大寫,因為make可以識別Makefile和makefile,用大寫可以鮮明一些)::
# String declaration<br />objects = main.o hello.o</p><p># Command<br />app : $(objects)<br />cc -o app $(objects)<br />main.o : main/main.c hello.h<br />cc -c main/main.c<br />hello.o : src/hello.c stdio.h<br />cc -c src/hello.c</p><p># Search paths<br />vpath %.h /usr/include inc</p><p># Clean the intermediate files<br />.PHONY : clean<br />clean :<br />rm app $(objects)<br />
hello.h:
void hello(char name[]);
main.c:
#include <stdio.h><br />#include "../inc/hello.h"<br />// The second hello.h should in ""<br />int main()<br />{<br /> hello("GCC");<br /> printf("Haha Linux Ubuntu!/n");<br /> return 0;<br />}
其中,第二個包含檔案,hello.h,必須要用"",如果用<>則gcc只會到系統目錄下去搜尋,不會到本目前的目錄下搜尋
就是""在使用者目錄下,<>在系統目錄下,這個在windows上不嚴格, 在linux裡似乎很嚴格
hello.c:
#include <stdio.h></p><p>void hello(char name[])<br />{<br />printf("Hello %s!/n", name);<br />}<br />