標籤:makefile
0.前言 從學習C語言開始就慢慢開始接觸makefile,查閱了很多的makefile的資料但總感覺沒有真正掌握makefile,如果自己動手寫一個makefile總覺得非常吃力。所以特意藉助部落格總結makefile的相關知識,通過例子說明makefile的具體用法。 例說makefile大致分為4個部分 1.只有單個C檔案 2.含有多個C檔案 3.需要包括標頭檔路徑 4.一個較為複雜的例子 【代碼倉庫】——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", a+b); printf("a-b=%d\n", 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_ADDint 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_SUBint sub(int a, int b);#endif
2.複習gcc指令 gcc指令可通過-I首碼指定標頭檔路徑,特別說明./代表當前路徑,../代表上一級目錄。
3.編寫makefile 請替換其中的[tab],並以代碼倉庫中的makefile檔案為主。
# 指令編譯器和選項CC=gccCFLAGS=-Wall -std=gnu99# 目標檔案TARGET=testSRCS = test.c ./test-add/test-add.c ./test-sub/test-sub.cINC = -I./test-add -I./test-subOBJS = $(SRCS:.c=.o)$(TARGET):$(OBJS)#@echo TARGET:[email protected]#@echo OBJECTS:$^[tab]$(CC) -o [email protected] $^clean:[tab]rm -rf $(TARGET) $(OBJS)%.o:%.c[tab]$(CC) $(CFLAGS) $(INC) -o [email protected] -c $<
【具體說明】 【1】相比於單個檔案和多個檔案的makefile,通過變數INC制定了標頭檔路徑。標頭檔路徑之間通過空格隔開。 【2】編譯規則%.o:%.c中加入了標頭檔參數$(CC) $(CFLAGS) $(INC) -o [email protected] -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.ogcc -Wall -std=gnu99 -I./test-add -I./test-sub -o test.o -c test.cgcc -Wall -std=gnu99 -I./test-add -I./test-sub -o test-add/test-add.o -c test-add/test-add.cgcc -Wall -std=gnu99 -I./test-add -I./test-sub -o test-sub/test-sub.o -c test-sub/test-sub.cgcc -o test test.o test-add/test-add.o test-sub/test-sub.o 從控制台的輸出可以看出,通過make clean清除上一次的可執行檔和目標檔案,然後依次編譯各個C檔案,在編譯的過程中制定了標頭檔路徑,最後把3個目標檔案連結為最終可執行檔。