好比有以下代碼: linux
#include<stdio.h> int main( void ){ int a = 0; int b = 0; puts("輸入兩個整數:"); scanf("%d %d",&a,&b); printf("%d + %d = %d\n",a,b,a+b); printf("\nHello World!\n小代碼!\n"); return 0; }
那麼個人makefile就是: c++
test:test.o gcc test.o -o test test.o:test.c gcc -c -Wall test.c
此時個人Makefile的做用也就是少打了一次命令而已. shell
makefile就是把編譯命令寫入到一個文本文件,可使用make命令解析這些命令之間的依賴關係,從而實現自動化編譯程序,固然前提是編寫的命令都是正確的.
數據庫
這個階段,是在我寫OJ題目的時候,由於那會兒還不會在gcc中指定頭文件目錄,若是遇到多個文件編譯,就只能在IDE中了,若是須要輸入,那麼只能在ide中編譯運行一次,沒有問題了,再在終端./app來執行測試. 編程
可是寫代碼的實力老是要提升的,那麼文件不可能老是一個.因而就有了相似於下面這三個文件的代碼結構 ubuntu
sum.h vim
#ifndef _SUM_H_ #define _SUM_H_ int sum( int a, int b ); #endif//_SUM_H_sum.c
#include"../include/sum.h" int sum( int a, int b ){ return a + b; }test.c
#include<stdio.h> #include"../include/sum.h" int main( void ){ int a = 0; int b = 0; puts("輸入兩個整數:"); scanf("%d %d",&a,&b); printf("%d + %d = %d\n",a,b,sum(a,b)); return 0; }
OBJS=test.o sum.o test:${OBJS} gcc ${OBJS} -o test test.o:test.c sum.h gcc -c -Wall test.c sum.o:sum.c sum.h gcc -c -Wall sum.c
這個階段大約在五天以前.使用了make的簡單變量,也就是:OBJS=test.o sum.o.其它的東西能夠說是一點沒有 app
在看了<GNU make項目管理>以後,我開始佈局個人源代碼,因而個人代碼目錄以下: eclipse
laolang@laolang-Lenovo-G470:~/code/makefile/blog/two$ tree . ├── include │ └── sum.h ├── Makefile └── src ├── sum.c └── test.c 2 directories, 4 files laolang@laolang-Lenovo-G470:~/code/makefile/blog/two$
OBJS=test.o sum.o CC=gcc CFLAGS=-Wall -I include vpath %.h include vpath %.c src test:${OBJS} gcc ${OBJS} -o test test.o:sum.h sum.o:sum.h .PHNOY: clean clean: -rm *.o test
此時開始初步使用make的隱含規則,僞目標,以及vpath.這個階段大概在兩天以前.此時已經能夠寫簡單的Makefile,使用僞目標來清除中間文件和生成的可執行程序. ide
這個階段也就是寫下這個記錄的時刻.基本上能夠編寫單一的Makefile了.可是對於make的變量的理解仍是遠遠的不足,並且以後幾天確定也會用到多個Makefile.另外看到<GNU make項目管理>的第三四章,就要有必定的shell script編程能力才行.這個Makefile和以前的沒有太大的改進,就是寫了註釋,多了幾個目標而已
laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ tree . ├── include │ └── sum.h ├── Makefile └── src ├── sum.c └── test.c 2 directories, 4 files laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$
test.c
#include<stdio.h> #include"../include/sum.h" int main( void ){ int a = 0; int b = 0; puts("輸入兩個整數:"); scanf("%d %d",&a,&b); printf("%d + %d = %d\n",a,b,sum(a,b)); return 0; }
sum.h
#ifndef _SUM_H_ #define _SUM_H_ int sum( int a, int b ); #endif//_SUM_H_
sum.c
#include"../include/sum.h" int sum( int a, int b ){ return a + b; }
# 功能: makefile筆記 # # 做者:小代碼 # 時間:2014年 11月 14日 星期五 15:12:51 CST # # 程序名稱 PROGRAM=test # 指定C編譯器 CC=gcc # 指定編譯選項 CFLAGS=-Wall -I include # 指定頭文件目錄 vpath %.h include # 指定源文件目錄 vpath %.c src # 指定刪除命令 RM=rm -rf # 幫助列表 define HELP_LIST echo "#make\t\t\t\t編譯程序" echo "#make runt\t\t\t運行程序" echo "#make rebuild\t\t\t從新構建" echo "#make clean\t\t\t清除中間文件和可執行程序" echo "#make help\t\t\t輸出此幫助列表" endef # 默認目標,生成主程序 all:${PROGRAM} # 從新構建 rebuild:clean all # 生成主程序 ${PROGRAM}:test.o sum.o tees.o:sum.h sum.o:sum.h .PHNOY:clean run help # 清除中間文件和可執行程序 clean: -${RM} ${PROGRAM} *.o # 運行程序 run: ./${PROGRAM} # 顯示幫助列表 help: @${HELP_LIST}
laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ tree . ├── include │ └── sum.h ├── Makefile └── src ├── sum.c └── test.c 2 directories, 4 files laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ make help #make 編譯程序 #make runt 運行程序 #make rebuild 從新構建 #make clean 清除中間文件和可執行程序 #make help 輸出此幫助列表 laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ make gcc -Wall -I include -c -o test.o src/test.c gcc -Wall -I include -c -o sum.o src/sum.c gcc test.o sum.o -o test laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ make run ./test 輸入兩個整數: 3 4 3 + 4 = 7 laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ make rebuild rm -rf test *.o gcc -Wall -I include -c -o test.o src/test.c gcc -Wall -I include -c -o sum.o src/sum.c gcc test.o sum.o -o test laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ tree . ├── include │ └── sum.h ├── Makefile ├── src │ ├── sum.c │ └── test.c ├── sum.o ├── test └── test.o 2 directories, 7 files laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ make clean rm -rf test *.o laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$ tree . ├── include │ └── sum.h ├── Makefile └── src ├── sum.c └── test.c 2 directories, 4 files laolang@laolang-Lenovo-G470:~/code/makefile/blog/three$
到了如今,簡單的Makefile已經能夠寫了.能夠初步使用隱含規則,make的變量的使用,.PHNOY僞目標的使用,make宏的使用
可是也只是在使用階段,對於它們的本質還不瞭解.好比從一個.c文件編譯爲.o文件的隱含規則,make的預約義變量的含義,以及多個Makefile的使用.這隻
是Makefile自己的.更多的是gcc的使用.好比如何使用gdb調試程序,如何引用第三方庫,如何創建一個本身的庫.還有就是shell script編程能力,這方面我仍是個小白.最後,就是linux命令行操做了,如今我也只是會簡單的用一下grep而已.這些都是下一步要學習的.
爲了學習這些,我選擇了libxml2這個第三方庫,程序就是使用xml文件做爲數據庫部分的學生信息管理系統.
開發方式:
Eclipse --寫源代碼,主要是由於有libxml2的代碼提示
gvim -- 寫Makefile
make -- 程序的編譯,調試,運行