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

后端 未结 3 1567
被撕碎了的回忆
被撕碎了的回忆 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 15:58

    Put the build products into different directory trees (whilst keeping one copy of the source of course). That way you are always just a short compile from an up-to-date build, be it debug or release (or even others). No possibility of confusion either.

    EDIT

    Sketch of the above.

    src := 1.c 2.c 3.c
    bare-objs := ${src:%.c=%.o}
    release-objs := ${bare-objs:%=Release/%}
    debug-objs := ${bare-objs:%=Debug/%}
    
    Release/prog: ${release-objs}
    Debug/prog: ${debug-objs}
    
    ${release-objs}: Release/%.o: %.c # You gotta lurve static pattern rules
        gcc -c $< -o $@
    
    ${debug-objs}: Debug/%.o: %.c
        gcc -c $< -o $@
    
    Release/prog Debug/prog:
        gcc $^ -o $@
    
    .PHONY: all
    all: Release/prog ; echo $@ Success
    
    .PHONY: debug
    debug: Debug/prog ; echo $@ Success
    

    (Disclaimer: not tested, nor even run through make.)

    There you go. It's even -j safe so you can do make -j5 all debug. There is a lot of obvious boiler plate just crying out for tidying up.

提交回复
热议问题