为什么这个makefile最后会删除两个.c文件?

时间:2015-10-17 19:57:04

标签: python c makefile cython

我有一个看起来像这样的Makefile:

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 $@

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在完成后会删除Game.c和BaseGame.c?最后一个命令甚至不在makefile中!

2 个答案:

答案 0 :(得分:2)

make保留中间文件(.c文件是中间文件)

使用

.PRECIOUS: <list of file names>

在makefile中

以下内容来自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. 

答案 1 :(得分:1)

你有没有注意到&#34; *。c&#34;在clean部分?

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