日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第2天.make的学习(第二部分)对伪目标的理解

發(fā)布時間:2023/12/4 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第2天.make的学习(第二部分)对伪目标的理解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、目標,依賴,命令也許大家覺得這個不重要,但今天我有了新的認識,所以寫了下來。這三個就是Makefile的全部,但今天我要重點說一下它的執(zhí)行順序。每個Makefile都有且只有一個終極目標,下設若干子目標,make的規(guī)則會檢查目標與依賴的時間戳,依賴中的某一個比目標新,說明目標已經(jīng)過時,需要更新。這里要著重說一下,make會將目標的依賴及依賴的依賴全部展開,然后才能決定是否需要更新終極目標。a.out/???? |??? \/?????? |???? \_? /???????_?????? \_/???????????? |??????????? \hello.o?????? world.o????? main.o_/???????????????????|????????????????? \_/????????????????????? |???????????????????? \hello.c?????????????world.c??????????????? main.c對于這個例子,相信大家都能寫出Makefile。make在解析Makefile時,就會生成上面的依賴樹,它會自底向上比較時間戳,若下層的新,則執(zhí)行上層目標所對應的命令,然后,依次執(zhí)行上上層的命令(因為,底層更新了,所以上面的都需要更新)。如果底層不比上層的新,則不執(zhí)行上層目標所對應的命令(這就是make的聰明之處)。例如:我們更改了hello.c文件,make在解析Makefile時,首先比較的不是a.out與hello.o誰更新(如果make真的這么做,那么make就會直接退出,因為自你上次執(zhí)行make時,a.out會比hello.o更新,而我們修改hello.c,并不會影響hello.o的時間戳),?而是依賴樹的最底端,hello.c與hello.o的時間戳,因為hello.c的修改,目標hello.o的命令會被執(zhí)行,進而觸發(fā)a.out所對應的命令。目標werld.o與main.o所對應的命令當然不會被執(zhí)行。make的輸出看上去是這個樣子的gcc -c hello.cgcc hello.o world.o main.o?????在make?的info文檔中有這樣一段話:(注意粗體字)3.9 How `make' Reads a Makefile
===============================GNU `make' does its work in two distinct phases.? During the first
phase it reads all the makefiles, included makefiles, etc. and
internalizes all the variables and their values, implicit and explicit
rules, and constructs a dependency graph of all the targets and their
prerequisites.? During the second phase, `make' uses these internal
structures to determine what targets will need to be rebuilt and to
invoke the rules necessary to do so.4 Writing Rules
***************A "rule" appears in the makefile and says when and how to remake
certain files, called the rule's "targets" (most often only one per
rule).? It lists the other files that are the "prerequisites" of the
target, and "commands" to use to create or update the target.The order of rules is not significant, except for determining the
"default goal": the target for `make' to consider, if you do not
otherwise specify one.? The default goal is the target of the first
rule in the first makefile.? If the first rule has multiple targets,
only the first target is taken as the default.? There are two
exceptions: a target starting with a period is not a default unless it
contains one or more slashes, `/', as well; and, a target that defines
a pattern rule has no effect on the default goal.? (*Note Defining and
Redefining Pattern Rules: Pattern Rules.)二、偽目標偽目標總被認為是過期的,總被認為需要被更新。偽目標又稱標簽,之所以這樣叫,是為了與真實的文件進行區(qū)別。那偽目標是如何工作的,我們可以將它當作普通文件,但不管出現(xiàn)在目標中,還是依賴中,都以為著它是將要被更新的普通文件。1、偽目標做目標,普通文件做依賴這是最常見的一種,典型的all偽目標? .PHONY:allall: prog1 prog2?????? #執(zhí)行make后,會一次生成兩個可執(zhí)行程序prog1:? a.c? ...command...prog2:? b.c ...command...分析:在這個Makefile中。.PHONY是第一個目標,但它以點開頭,不會作為終極目標(在上面的英文中有解釋),所以終極目標是all,make的職責所在就是重建這個all目標。因為all是偽目標,make命令總認為它需要被更新,而更新它,需要首先更新prog1 和 prog2 ,這就達到了一次make而生成兩個可執(zhí)行文件。2、偽目標做目標,偽目標又做依賴(雖然稱為偽目標,但也可做依賴,目標與依賴是相互轉(zhuǎn)化的,就像上面的hello.o,它是hello.c的目標,又是a.out的依賴).PHONY: all subdir appsall: subdir appssubdir: prog1 prog2apps:? prog3 prog4prog1: a.ccommand...???prog2: b.ccommand...prog3: c.ccommand...prog4: d.ccommand...分析:all是終極目標,它依賴于subdir 和 apps ,因這兩個都是偽目標,即:他們需要被更新。subdir又依賴于prog1 和 prog2 這就回到了上面的那種情況。這種形式經(jīng)常出現(xiàn)在大的工程中,是對上面一種情況的擴展,我們可以修改all 或 subdir 或? apps 中的依賴,來決定我們需要那些應用程序,裁剪掉那些應用程序。你應該聽說過內(nèi)核裁剪與移植吧,所謂裁剪,就是修改Makefile的目標,不生成我們不需要的東東;所謂移植,就是修改Makefile的編譯參數(shù),換成其他的編譯工具。3、普通文件做目標,偽目標做依賴all: progprog: a.c prtcommand....PHONY: prtprt:@echo "this is prt"分析:每次執(zhí)行make時,this is prt都會出現(xiàn)在屏幕上。盡管prog不需要被更新,但prt中的命令依然要被執(zhí)行。make會這樣來解析,prog依賴于prt,而prt有需要被更新(因為它是偽目標),這意味著this si prt會出現(xiàn)在屏幕上,prt更新完成后,prog目標中的command...就會被執(zhí)行(我們假定a.c沒有被更改)。事實上我們那個文件也沒有更改,但是prog目標中的命令還是被執(zhí)行了,info make中不建議這樣做A phony target should not be a prerequisite of a real target file;
if it is, its commands are run every time `make' goes to update that
file.總結:在偽目標中始終貫穿著這樣一句話:偽目標總被認為是需要被更新的。我也是依據(jù)這句話來分析上述偽目標的應用。偽目標可以與真實文件重名,典型的clean用法,因為make知道,我們只是想執(zhí)行clean下的命令,而不是去重建一個叫做clean的文件,這也是我們有時把它解釋稱標簽的原因。關于上述第二種情況,info make中有這樣一個解釋When one phony target is a prerequisite of another, it serves as a
subroutine?of the other.? For example, here `make cleanall' will delete
the object files, the difference files, and the file `program':.PHONY: cleanall cleanobj cleandiffcleanall : cleanobj cleandiff
???????????? rm programcleanobj :
???????????? rm *.ocleandiff :
???????????? rm *.diff
一、目標,依賴,命令? ? 這三個是makefile的全部內(nèi)容,現(xiàn)在來說一下它的執(zhí)行順序

轉(zhuǎn)載于:https://www.cnblogs.com/SFTD/p/3622126.html

總結

以上是生活随笔為你收集整理的第2天.make的学习(第二部分)对伪目标的理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。