2012-03-09 72 views
26

我有一個軟件堆棧,可以創建一些中間文件作爲構建過程的一部分。有一些問題出現,構建中斷。我想看看那些中間生成的文件。令我驚訝的是,這些文件作爲構建過程的一部分被刪除。如何撤銷中間文件刪除

Removing intermediate files... 
rm fact_test_without_proxies.c fact_test_main.c fact_test_without_proxies.o 

我經歷了Makefiles我沒有看到任何明確的規則刪除它們。是否有任何隱式規則來刪除中間文件。如果是,我怎麼能禁用這些隱式規則?

只有在使用--debug選項執行make時,纔會看到打印Removing intermediate files...

[email protected]:~/coding/factorial/ut$ make --debug 
GNU Make 3.81 
Copyright (C) 2006 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. 
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. 

This program built for x86_64-pc-linux-gnu 
Reading makefiles... 
Updating goal targets.... 
File `check' does not exist. 
    File `test_dept_run' does not exist. 
    File `fact_test' does not exist. 
     File `fact_using_proxies.o' does not exist. 
      File `fact_test_without_proxies' does not exist. 
      File `fact_test_without_proxies.o' does not exist. 
      File `fact_test_without_proxies.c' does not exist. 
       File `fact_test_main.c' does not exist. 
      Must remake target `fact_test_main.c'. 
nm -p fact_test.o | build_main_from_symbols >fact_test_main.c 
      Successfully remade target file `fact_test_main.c'. 
      Must remake target `fact_test_without_proxies.c'. 
cp fact_test_main.c fact_test_without_proxies.c 
      Successfully remade target file `fact_test_without_proxies.c'. 
      Must remake target `fact_test_without_proxies.o'. 
gcc -I../src -c -o fact_test_without_proxies.o fact_test_without_proxies.c 
      Successfully remade target file `fact_test_without_proxies.o'. 
      Must remake target `fact_test_without_proxies'. 
gcc fact_test_without_proxies.o fact.o fact_test.o -o fact_test_without_proxies 
fact.o: In function `unknown': 
fact.c:(.text+0x67): undefined reference to `do_update' 
collect2: ld returned 1 exit status 
make: *** [fact_test_without_proxies] Error 1 
Removing intermediate files... 
rm fact_test_without_proxies.c fact_test_main.c fact_test_without_proxies.o 

回答

29

如果您使用gnumake的,您可以使用特殊目標.PRECIOUS

.PRECIOUS: fact_test_without_proxies.c fact_test_main.c fact_test_without_proxies.o 

或只是

.PRECIOUS: %.c %.o 

它唯一的作用是,這些文件不會被刪除,如果Make被殺或中斷。

+0

謝謝,這確實有助於:) – Kamath 2012-03-12 05:33:00

+5

不僅如此,使用.PRECIOUS意味着即使Make成功完成(即未被殺死或中斷),這些文件也不會被刪除。 「另外,如果目標是一箇中間文件,在不再需要它的時候不會被刪除,正如通常所做的那樣[...]在後一個方面,它與.SECONDARY特殊目標重疊。」 - https://www.gnu.org/software/make/manual/make.html – PonyEars 2014-06-19 00:50:28

+2

+1:與'.SECONDARY'不同,這是POSIX:http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ make.html – 2015-03-18 05:09:07

35

您還可以使用.SECONDARY,即使構建沒有中斷,它也會保留指定的文件。

例如

.SECONDARY: 
+0

謝謝。只需補充一點,.PRECIOUS「與.SECONDARY特殊目標重疊」。關於保留指定的文件,即使構建不會中斷(引自https://www.gnu.org/software/make/manual/make.html) – PonyEars 2014-06-19 00:50:56

+4

您應該將參數列出到'.SECONDARY'。如果沒有先決條件,它會導致所有*中間文件被保留,這不一定就是你想要的。 – Kaz 2014-12-05 00:29:19

+0

謝謝!不幸的是,'.SECONDARY'中的列出目標似乎在​​除了'.o'之外的擴展中被破壞。無論如何,'.d'依賴文件都會被刪除。這個解決方案,雖然不知道,但至少可以工作:) – foges 2015-12-08 19:37:31

3

有關於使用指標的限制,從而影響.PRECIOUS的行爲:

我的目標A /%FOO:和B/FOO%。,所以我已經設置:

.PRECIOUS: %.foo 

並且這沒有奏效;我不明白爲什麼,但擴張不是這樣工作的;我必須明確地列出目標,正是因爲他們寫:

.PRECIOUS: A/%.foo B/%.foo 

但即使讀https://www.gnu.org/software/make/manual/html_node/Special-Targets.html後,我不明白.PRECIOUS之間的區別:和.SECONDARY:。

它被接受使用這些特殊的目標沒有依賴,但我認爲這將是非常髒的編碼,並會期望副作用。有些人只是把.PRECIOUS:或.SECONDARY:沒有dep,後來,他們抱怨說,他們必須運行後打破打造乾淨...