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



我有一个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中!

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

使用

.PRECIOUS: <list of file names>
makefile 中的

以下内容来自https://www.gnu.org/software/make/manual/html_node/Special-Targets.html

。珍贵的

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. 

您注意到clean部分中的"*.c"了吗?

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

最新更新