如何强制重新编译Makefile中的单个文件?

时间:2010-07-22 00:31:48

标签: c++ c makefile autotools

我们的想法是项目中包含一个__DATE____TIME__的文件。在不明确更改其修改日期的情况下重新编译它可能会很酷。

编辑:$(shell touch -c ..)如果只是笨拙可能是一个很好的解决方案。

3 个答案:

答案 0 :(得分:8)

标准习惯用法是让目标文件(不是源文件!)依赖于不存在且没有规则或依赖的目标(此目标通常称为FORCE),如下所示

always-recompile.o: FORCE
FORCE:

如果以某种方式创建名为“FORCE”的文件,这将会中断。使用GNU make,您可以使用特殊目标.PHONY,它没有此限制,但确实需要您有一个明确的规则来重建该文件:

always-recompile.o:
        $(CC) $(CFLAGS) -c -o always-recompile.o always-recompile.c

.PHONY: always-recompile.o

有关详细信息,请参阅http://www.gnu.org/software/make/manual/html_node/Phony-Targets.html

答案 1 :(得分:1)

执行此操作的一种方法是在运行.o之前删除相应的目标文件(.objmake)。这将触发重新编译(和重新链接)而不更改源文件修改日期。

答案 2 :(得分:0)

2 4 6 

无法理解正在发生什么,如上调用,make将永远不会编译SDL2_etape1.c ...

我尝试触摸,make -B,make --always-make,我修改了源代码,makefile,没办法...

这是makefile的内容

sirius:/exemples/sdl2/cours # make SDL2_etape1
make: 'SDL2_etape1' is up to date.
sirius:/exemples/sdl2/cours # ls -lah
total 96K
drwxr-xr-x 1 root root  316 Mar 19 20:46 .
drwxr-xr-x 1 root root   34 Apr  6  2016 ..
-rw-r--r-- 1 root root 2.5K Mar 19 20:45 SDL2_etape1.c
-rwxr-xr-x 1 root root  17K Mar 19 20:45 SDL2_etape2
-rw-r--r-- 1 root root 3.1K Mar 19 20:45 SDL2_etape2.c
-rw-r--r-- 1 root root 4.3K Mar 19 20:45 SDL2_etape3.c
-rw-r--r-- 1 root root 5.2K Mar 19 20:45 SDL2_etape4.c
-rw-r--r-- 1 root root 5.7K Mar 19 20:45 SDL2_etape5.c
-rw-r--r-- 1 root root 7.4K Mar 19 20:45 SDL2_etape6.c
-rw-r--r-- 1 root root  18K Mar 19 20:45 exemple_type_fonction.c
drwxr-xr-x 1 root root   42 Apr 30  2016 images
-rw-r--r-- 1 root root 5.3K Mar 19 20:45 ldf_SDL2addshadedtext.c
-rw-r--r-- 1 root root  127 Mar 19 20:46 makefile
-rw-r--r-- 1 root root  137 Mar 19 20:33 makefile~

我注意到,使用shell补全(Tab键),“ make SDL2_etape2”可以正常工作……就像该文件夹中的所有其他文件一样……

sirius:/exemples/sdl2/cours # more makefile
CFLAGS=`pkg-config SDL2_ttf SDL2_image --cflags`
LDFLAGS=`pkg-config SDL2_ttf SDL2_image --libs`

SDL2_etape1: SDL2_etape1.c