Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you force recompilation of a single file in a Makefile?

The idea is that a project has a single file with __DATE__ and __TIME__ in it. It might be cool to have it recompiled without explicitly changing its modification date.

edit: $(shell touch -c ..) might be a good solution if only clumsy.

like image 641
j riv Avatar asked Jul 22 '10 00:07

j riv


2 Answers

The standard idiom is to have the object file (not the source file!) depend on a target which doesn't exist and has no rules or dependencies (this target is conventionally called FORCE), like this

always-recompile.o: FORCE
FORCE:

This will break if a file named "FORCE" gets created somehow, though. With GNU make you can instead use the special target .PHONY, which doesn't have this limitation, but does require you to have an explicit rule to rebuild that file:

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

.PHONY: always-recompile.o

See http://www.gnu.org/software/make/manual/html_node/Phony-Targets.html for more details.

like image 197
zwol Avatar answered Sep 19 '22 05:09

zwol


One way to do this is to delete the corresponding object file (.o or .obj) before running make. This will trigger a recompile (and relink) without changing the source file modification date.

like image 35
Greg Hewgill Avatar answered Sep 22 '22 05:09

Greg Hewgill