How to force make to always rebuild a file

后端 未结 4 1053
野的像风
野的像风 2021-02-01 16:16

I have a version.c file in my project that contains current revision of the project and some other stuff that is passed as a definition (-D compiler option) from makefile.

相关标签:
4条回答
  • 2021-02-01 17:04

    The classic way to do it is:

    version.o:   .FORCE
    
    .FORCE:
    

    (and you might add .PHONY: .FORCE). The file '.FORCE' is presumed not to exist, so it is always 'created', so version.o is always out of date w.r.t it, so version.o is always compiled.

    I'm not sure that making version.o into a phony file is correct; it is actually a real file, not a phony one.

    0 讨论(0)
  • 2021-02-01 17:05

    If you want to do this using the FORCE mechanism the correct solution looks like this:

    version.o: FORCE
    
    .PHONY: FORCE
    FORCE:
    

    By explicitly declaring FORCE to be phony we make sure things will work right even if .SECONDARY: is used (.SECONDARY: will cause FORCE to be considered an intermediate file, and make doesn't rebuilt intermediate files unless they have prerequisites newer than the ultimate target, and FORCE doesn't have any prerequisites, so .PHONY: FORCE is needed).

    The other solution (using $(shell touch version.c)) also has a problem: it may cause your editor to think version.c has been updated, and prompt for a reload of the file, which might end up being destructive if you've been editing the file's buffer but haven't yet saved it. If you don't mind this, it can be made even simpler by observing that the touch command is silent, so the assignment to the hack dummy variable isn't needed:

    $(shell touch version.c)  # This is enough, but will likely confuse your editor
    

    The .PHONY "trick" referred to in the comments on the question generally DOES NOT work. It may look like it does because it will force a relink iff version.o already exists, but the actual object file won't get rebuilt if the .o file rule is an implicit rule (which it usually is). The problem is that make doesn't do the implicit rule search for explicitly phony targets. This make file shows the failure:

    fooprog: test.o
            cp $< $@
    
    %.o: %.c
            cp $< $@
    
    .PHONY: test.o # WRONG
    
    clean:
            rm test.o fooprog
    

    If a static pattern rule is used instead of an implicit rule the .PHONY: version.o trick will work. In general using static pattern rules instead of implicit rules cuts out most of the more confusing Make behaviors. But most make files use implicit rules.

    0 讨论(0)
  • 2021-02-01 17:09

    Not a makefile way, but easier than touch:

    make -B
    

    ‘-B’ ‘--always-make’

    Consider all targets out-of-date. GNU make proceeds to consider targets and their prerequisites using the normal algorithms; however, all targets so considered are always remade regardless of the status of their prerequisites. To avoid infinite recursion, if MAKE_RESTARTS (see Other Special Variables) is set to a number greater than 0 this option is disabled when considering whether to remake makefiles (see How Makefiles Are Remade).

    0 讨论(0)
  • 2021-02-01 17:17

    The quick hack version when you just need it to work and you don't want to play Make games:

    # Hack to get main.c rebuilt
    hack := $(shell touch main.c)
    

    Basically just make Make run touch for you.

    0 讨论(0)
提交回复
热议问题