2015-10-17 76 views
0

我有一個Makefile文件,看起來像這樣:爲什麼這個makefile在最後刪除兩個.c文件?

TARGET = Game 
OBJ = Game.o BaseGame.o main.o 

PFLAGS = -a 
CFLAGS = -c -I/usr/include/python2.7/ -Wall -std=c11 
LFLAGS = -lpython2.7 
CC = gcc 

all: $(TARGET) 

$(TARGET): $(OBJ) 
    $(CC) $(OBJ) $(LFLAGS) -o $(TARGET) 

%.o: %.c 
    $(CC) $< $(CFLAGS) -o [email protected] 

main.c: 
    cython main.py $(PFLAGS) --embed 

%.c: %.py 
    cython $< $(PFLAGS) 

clean: 
    rm -f *.o *.c html/* $(TARGET) 

當我運行「make」在終端上,這是輸出:

cython Game.py -a 
gcc Game.c -c -I/usr/include/python2.7/ -Wall -std=c11 -o Game.o 
cython BaseGame.py -a 
gcc BaseGame.c -c -I/usr/include/python2.7/ -Wall -std=c11 -o BaseGame.o 
cython main.py -a --embed 
gcc main.c -c -I/usr/include/python2.7/ -Wall -std=c11 -o main.o 
gcc Game.o BaseGame.o main.o -lpython2.7 -o Game 
rm Game.c BaseGame.c 

我的問題是,爲什麼makefile文件刪除遊戲.c和BaseGame.c完成後?最後的命令甚至不在makefile中!

回答

2

具有make保持中間文件(.c文件是中間文件)

使用

.PRECIOUS: <list of file names> 

在生成文件

下面是從https://www.gnu.org/software/make/manual/html_node/Special-Targets.html

.PRECIOUS

The targets which .PRECIOUS depends on are given the following special treatment: if make is killed or interrupted during the execution of their recipes, the target is not deleted. See Interrupting or Killing make. Also, if the target is an intermediate file, it will not be deleted after it is no longer needed, as is normally done. See Chains of Implicit Rules. In this latter respect it overlaps with the .SECONDARY special target. 

You can also list the target pattern of an implicit rule (such as ‘%.o’) as a prerequisite file of the special target .PRECIOUS to preserve intermediate files created by rules whose target patterns match that file’s name. 
+0

感謝您的幫助,這個作品! +1 – Dovahkiin

1

您是否注意到clean部分中的「* .c」?

clean: 
    rm -f *.o *.c html/* $(TARGET) 
+0

是的,但是如果makefile運行的是乾淨的部分,那麼它會刪除所有內容,而不僅僅是兩個看似隨機的.c文件。 – Dovahkiin

相關問題