從學習C語言開始就慢慢開始接觸makefile,查閱了非常多的makefile的資料但總感覺沒有真正掌握makefile。假設自己動手寫一個makefile總認為非常吃力。
所以特意借助博客總結makefile的相關知識。通過樣例說明makefile的詳細使用方法。
例說makefile分為下面幾個部分。很多其它內容請參考【
例說makefile索引博文】
2.含有多個C文件
3.須要包含頭文件路徑
4.添加宏定義
5.添加系統共享庫
6.添加自己定義共享庫
7.一個實際的樣例
【代碼倉庫】——
makefile-example
代碼倉庫位於bitbucket,可借助TortoiseHg(GUI工具)克隆代碼或者在網頁中直接下載zip包。
【本例說明】
本例將說明makefile文件里怎樣增加宏定義。
1.gcc復習
宏定義使用前綴-D,在編譯過程中能夠把宏定義追加到CFLAG中。宏定義有兩種相似的寫法
【第一種】-D DEFINES
【另外一種】-D DEFINES=CONDITION
2.源文件
使用兩種不同的方式,通過宏定義包裹打印功能,分別使用#ifdef和#if
#include <stdio.h> #include <test-add.h> #include <test-sub.h> int main(void) { int a = 3; int b = 2; printf("a=%d\n", a); printf("b=%d\n", b); #ifdef TEST_ADD printf("a+b=%d\n", add(a,b)); #endif #if TEST_SUB printf("a-b=%d\n", sub(a,b)); #endif return 0; }
3.makefile
請替換當中的[tab],並以代碼倉庫中的makefile文件為主。
# 指令編譯器和選項 CC=gcc CFLAGS=-Wall -std=gnu99 # 宏定義 DEFS = -DTEST_ADD -DTEST_SUB=1 CFLAGS += $(DEFS) # 目標文件 TARGET=test # 源文件 SRCS = test.c \ ./test-add/test-add.c \ ./test-sub/test-sub.c # 頭文件查找路徑 INC = -I./test-add -I./test-sub # 目標文件 OBJS = $(SRCS:.c=.o) # 鏈接為可運行文件 $(TARGET):$(OBJS) # @echo TARGET:$@ # @echo OBJECTS:$^ [tab]$(CC) -o $@ $^ clean: [tab]rm -rf $(TARGET) $(OBJS) # 連續動作,請清除再編譯鏈接。最后運行 exec:clean $(TARGET) [tab]@echo 開始運行 [tab]./$(TARGET) [tab]@echo 運行結束 # 編譯規則 $@代表目標文件 $< 代表第一個依賴文件 %.o:%.c [tab]$(CC) $(CFLAGS) $(INC) -o $@ -c $<
4.詳細說明
【1】 makefile定義頭文件的方法有兩種
【第一種】-D DEFINES
【另外一種】-D DEFINES=CONDITION
當中第一種方法相應
#ifdef
do_something()
#endif
另外一種方法相應
#ifndef DEFINES
do_something()
#endif
【2】DEFS = -DTEST_ADD -DTEST_SUB=1
為了說明問題。此處使用了兩種不同的寫法。此時兩處打印功能均被運行
【3】CFLAGS += $(DEFS)
追加到CFLAGS中。此處須要強調CFLAGS僅僅是一個變量,能夠命名為不論什么合法的名稱,僅僅要在編譯過程中引用該參數就可以。
$(CC)
$(CFLAGS) $(INC) -o $@ -c $<
5.運行過程
【編譯和鏈接】
make clean && make
【控制台輸出】
rm -rf test test.o ./test-add/test-add.o ./test-sub/test-sub.o
gcc -Wall -std=gnu99 -DTEST_ADD -DTEST_SUB=1 -I./test-add -I./test-sub -o test.o -c test.c
gcc -Wall -std=gnu99 -DTEST_ADD -DTEST_SUB=1 -I./test-add -I./test-sub -o test-add/test-add.o -c test-add/test-add.c
gcc -Wall -std=gnu99 -DTEST_ADD -DTEST_SUB=1 -I./test-add -I./test-sub -o test-sub/test-sub.o -c test-sub/test-sub.c
gcc -o test test.o test-add/test-add.o test-sub/test-sub.o
gcc -Wall -std=gnu99 -DTEST_ADD -DTEST_SUB=1 -I./test-add -I./test-sub -o test.o -c test.c
gcc -Wall -std=gnu99 -DTEST_ADD -DTEST_SUB=1 -I./test-add -I./test-sub -o test-add/test-add.o -c test-add/test-add.c
gcc -Wall -std=gnu99 -DTEST_ADD -DTEST_SUB=1 -I./test-add -I./test-sub -o test-sub/test-sub.o -c test-sub/test-sub.c
gcc -o test test.o test-add/test-add.o test-sub/test-sub.o
從控制台的輸出能夠看出,在編譯過程中增加了-D參數。
【運行】
a=3
b=2
a+b=5
a-b=1
b=2
a+b=5
a-b=1
終於效果和預期全然同樣,makefile得到的驗證。
6.總結
【1】添加宏兩種方法定義 -D DEFINES 和 -D DEFINES=CONDITION
【2】附加到宏定義CFLAG之后