为什么这个makefile在最后删除两个.c文件?
问题描述:
我有一个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中!
答
具有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.
答
您是否注意到clean
部分中的“* .c”?
clean:
rm -f *.o *.c html/* $(TARGET)
+0
是的,但是如果makefile运行的是干净的部分,那么它会删除所有内容,而不仅仅是两个看似随机的.c文件。 – Dovahkiin
感谢您的帮助,这个作品! +1 – Dovahkiin