Posted on 2010-02-05 15:51
Prayer 閱讀(670)
評論(0) 編輯 收藏 引用 所屬分類:
Shell
Makefile中使用隱含規(guī)則來編譯程序
本示例演示一個程序的生成過程,使用的程序文件為第4章中的文件。Makefile文件和程序文件在一個文件夾中。Makefile文件如下所示:
CC := gcc HEAD := getarg.h SRC := getarg.c writeinfo.o main.c OBJS := getarg.o writeinfo.o main.o TT := test
Files := $(wildcard ./*)
INC = . CFLAGS = -pipe -g -Wall -I$(INC) LDFLAGS = -Wall -g
all:$(TT)
$(TT):$(OBJS) @echo "+++++++ Build Standalone Programe : $@ +++++++" $(CC) $(LDFLAGS) $(OBJS) -o $@
libtest_d.so:getarg.o writeinfo.o @echo "+++++++ Build Dynamic lib : $@ +++++++" $(CC) -shared $(LDFLAGS) getarg.o writeinfo.o -o $@
test_dlib:libtest_d.so main.o @echo "+++++++ Build Exe by Dynamic lib : $@ +++++++" $(CC) $(LDFLAGS) main.o -L. -ltest_d -o $@
filelist: @echo "<<<<<<< Files in this folder >>>>>>" @file $(Files)
.PHONY : clean filelist
%.o:%c $(CC) $(CFLAGS) -c $< -o $@
clean: @echo "------- clean ------" rm -f *.o rm -f $(TT) rm -f libtest_d.so rm -f test_dlib
|
在本例中,定義了CC等變量,在變量引用和使用這些變量的時候,需要用$(CC)的形式。
在Makefile和源文件所在目錄中,在命令行執(zhí)行make命令:
執(zhí)行的結(jié)果如下所示:
gcc -pipe -g -Wall -I. -c -o getarg.o getarg.c gcc -pipe -g -Wall -I. -c -o writeinfo.o writeinfo.c gcc -pipe -g -Wall -I. -c -o main.o main.c +++++++ Build Standalone Programe : test +++++++ gcc -Wall -g getarg.o writeinfo.o main.o -o test
|
在執(zhí)行的過程中,默認(rèn)執(zhí)行all目標(biāo),由于all目標(biāo)依賴于變量$(TT),$(TT)實際上是test。$(TT)依賴于$(OBJS),$(OBJS)就是getarg.o writeinfo.o main.o。因此,需要產(chǎn)生這三個目標(biāo)文件。
上述make工作的處理過程是這樣的:首先尋找三個目標(biāo)文件(getarg.o,writeinfo.o和main.o)的生成規(guī)則。在所有的規(guī)則中,并沒有這三個目標(biāo)文件的生成規(guī)則,因此使用默認(rèn)的目標(biāo)%.o:%c中的規(guī)則生成這三個目標(biāo)文件。這個時候會使用gcc編譯生成這三個目標(biāo)文件。生成完三個目標(biāo)文件之后,將執(zhí)行test目標(biāo),進行目標(biāo)文件的連接。
事實上,上述執(zhí)行過程中只是直接執(zhí)行了all目標(biāo),在Makefile中還有l(wèi)ibtest_d.so、test_dlib和filelist幾個目標(biāo)沒有執(zhí)行,而這些目標(biāo)可以單獨執(zhí)行。
執(zhí)行單獨的目標(biāo)filelist:
顯示的結(jié)果如下:
<<<<<<< Files in this folder >>>>>> ./getarg.c: ASCII C program text ./getarg.h: ASCII text ./getarg.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped ./main.c: ASCII C program text ./main.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped ./Makefile: ASCII make commands text ./test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), for GNU/Linux 2.6.4, not stripped ./writeinfo.c: ASCII C program text ./writeinfo.h: ASCII text ./writeinfo.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
|
這條目標(biāo)執(zhí)行的命令是使用file命令查看本文件夾下所有的文件。其中,F(xiàn)iles := $(wildcard ./*)表示使用通配符尋找目錄下的所有文件。
執(zhí)行生成可執(zhí)行程序test_dlib的命令:
執(zhí)行的結(jié)果如下所示:
+++++++ Build Dynamic lib : libtest_d.so +++++++ gcc -shared -Wall -g getarg.o writeinfo.o -o libtest_d.so +++++++ Build Exe by Dynamic lib : test_dlib +++++++ gcc -Wall -g main.o -L. -ltest_d -o test_dlib
|
test_dlib目標(biāo)是一個可執(zhí)行程序,它本身需要連接一個動態(tài)庫libtest_d.so,因此它依賴于目標(biāo)libtest_d.so和main.o目標(biāo),由于main.o已經(jīng)生成,這樣還需要生成libtest_d.so目標(biāo)。在libtest_d.so目標(biāo)中,依賴的文件getarg.o和writeinfo.o都已經(jīng)生成了,因此直接生成這個動態(tài)庫即可。libtest_d.so生成后,再生成test_dlib可執(zhí)行程序。
在以上的示例中的test和test_dlib都是可執(zhí)行的程序,它們的區(qū)別在于前者包含了三個目標(biāo)文件,可以直接執(zhí)行,后者只包括了main.o一個目標(biāo)文件,它的執(zhí)行必須依賴動態(tài)庫。
繼續(xù)使用clean清除目標(biāo):
執(zhí)行的結(jié)果如下所示:
------- clean ------ rm -f *.o rm -f test rm -f libtest_d.so rm -f test_dlib
|
在清除目標(biāo)之后,生成test_dlib可執(zhí)行程序:
$ make test_dlib gcc -pipe -g -Wall -I. -c -o getarg.o getarg.c gcc -pipe -g -Wall -I. -c -o writeinfo.o writeinfo.c +++++++ Build Dynamic lib : libtest_d.so +++++++ gcc -shared -Wall -g getarg.o writeinfo.o -o libtest_d.so gcc -pipe -g -Wall -I. -c -o main.o main.c +++++++ Build Exe by Dynamic lib : test_dlib +++++++ gcc -Wall -g main.o -L. -ltest_d -o test_dlib
|
在這次執(zhí)行的過程中,由于getarg.o,writeinfo.o和main.o三個目標(biāo)文件還沒有生成,因此在生成庫libtest_d.so之前,需要先編譯生成getarg.o和writeinfo.o兩個目標(biāo),它們使用的是默認(rèn)的規(guī)則。在libtest_d.so生成后,還需要生成main.o的過程,它也需要使用默認(rèn)的規(guī)則。
知識點:通常情況下,為了加速開發(fā),程序員自己編寫的程序在無特殊要求下都可以使用隱含規(guī)則,這樣還可以防止因為Makefile編寫錯誤而導(dǎo)致程序運行錯誤。