0.前言
從學習C語言開始就慢慢開始接觸makefile,查閱了非常多的makefile的資料但總感覺沒有真正掌握makefile,假設自己動手寫一個makefile總認為非常吃力。所以特意借助博客總結makefile的相關知識,通過樣例說明makefile的詳細使用方法。
例說makefile分為下面幾個部分,很多其它內容請參考【
例說makefile索引博文】
2.含有多個C文件
3.須要包含頭文件路徑
4.添加宏定義
5.添加系統共享庫
6.添加自己定義共享庫
7.一個實際的樣例
【代碼倉庫】——
makefile-example
代碼倉庫位於bitbucket,可借助TortoiseHg(GUI工具)克隆代碼或者在網頁中直接下載zip包。
1.三個C文件和三個頭文件
此處的樣例略微復雜些但更接近實際情況。
文件結果例如以下:根目錄中包括test.c makefileh和目錄test-add和目錄test-sub。
test.c makefile
【test-add】test-add.c test-add.h
【test-sub】test-sub.c test-sub.h
【test.c】
#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); printf("a+b=%d\n", add(a,b)); printf("a-b=%d\n", sub(a,b)); return 0; }
【test-add.c】
#include <test-add.h> int add(int a, int b) { return a+b; }
【test-add.h】
#ifndef __TEST_ADD int add(int a, int b); #endif
【test-sub.c】
#include "test-sub.h" int sub(int a, int b) { return a-b; }
【test-sub.h】
#ifndef __TEST_SUB int sub(int a, int b); #endif
2.復習gcc指令
gcc指令可通過-I前綴指定頭文件路徑,特別說明./代表當前路徑,../代表上一級文件夾。
3.編寫makefile
請替換當中的[tab],並以代碼倉庫中的makefile文件為主。
# 指令編譯器和選項 CC=gcc CFLAGS=-Wall -std=gnu99 # 目標文件 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) %.o:%.c [tab]$(CC) $(CFLAGS) $(INC) -o $@ -c $<
【詳細說明】
【1】相比於單個文件和多個文件的makefile,通過變量INC制定了頭文件路徑。頭文件路徑之間通過空格隔開。
【2】編譯規則%.o:%.c中增加了頭文件參數$(CC) $(CFLAGS)
$(INC) -o $@ -c $<,那么在編譯的過程中便會出現
gcc -Wall -std=gnu99
-I./test-add -I./test-sub -o test.o -c test.c。和單個文件和多個文件的makefile相比添加了頭文件路徑參數。
【3】SRCS變量中,文件較多時可通過“\”符號續行。
【編譯】
make clean && make
【控制台輸出】
rm -rf test test.o ./test-add/test-add.o ./test-sub/test-sub.o
gcc -Wall -std=gnu99
-I./test-add -I./test-sub -o test.o -c test.c
gcc -Wall -std=gnu99
-I./test-add -I./test-sub -o test-add/test-add.o -c test-add/test-add.c
gcc -Wall -std=gnu99
-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
從控制台的輸出能夠看出,通過make clean清除上一次的可運行文件和目標文件,然后依次編譯各個C文件,在編譯的過程中制定了
頭文件路徑,最后把3個目標文件鏈接為終於可運行文件。