讀書心得 Managing Projects with GNU Make (2-1) Phony Target

本文出自 Managing Projects with GNU Make 3rd-Robert Mecklenburg(2005)

Chp2. Rules


Phony Targets:

有的 target 並不是實體的檔案,這種 target 我們稱為 Phony Target(假的 target)

clean:
        rm -f *.o *.out   # 清除所有 build 出來的檔案

在這邊 clean 不是實體的檔案所以 clean 是一個 Phony Target。先創造一個檔案名為clean 再 make clean 看看會怎樣吧

$ touch clean
$ make clean
make: 'clean' is up to date.

下面是 GNU make 有關 Phony Target 的官方連結:

https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html

在這邊 make clean 的時候 make 會試著判斷有沒有 clean 這個檔案,如果有的話就會判斷 clean 是 up to date 的狀態

解決這個問題的方法式是明確地指出 clean 是一個 Phony Target 不是一個檔案:

.PHONY: clean
clean:
        rm -f *.o lexer.c

最後舉個 Phony Target 需要 prerequisite 的例子:

.PHONY: cleanall cleanobj cleandiff
cleanall : cleanobj cleandiff
        rm program

cleanobj :
        rm *.o

cleandiff :
        rm *.diff

留言

熱門文章