How do you implement a Makefile that remembers the last build target?

后端 未结 3 1535
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 15:40

Let\'s say you have a Makefile with two pseudo-targets, \'all\' and \'debug\'. The \'debug\' target is meant to build the same project as \'all\', except with some different

3条回答
  •  别那么骄傲
    2021-01-05 16:09

    Keeping variant sets of object files (as in bobbogo's solution) is probably the best way, but if for some reason you don't want to do that, you can use empty files as markers, to indicate which way you last built the executable:

    %-marker:
            @rm -f $(OBJECTS) *-marker
            @touch $@
    
    debug: GCCFLAGS += -ggdb
    
    debug: SOMEOTHERFLAG = WHATEVER
    
    all debug: % : %-marker
            @echo making $@
            @$(MAKE) -S GCCFLAGS='$(GCCFLAGS)' SOMEOTHERFLAG='$(SOMEOTHERFLAG)' main
    

    There are other variants on this idea; you could have a small file containing the flag settings, which the makefile would build and include. That would be clever, but not really any cleaner than this.

提交回复
热议问题