本文介绍了如何实现一个记住最后一个构建目标的Makefile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个带有两个伪目标的Makefile,分别是"all"和"debug". 调试"目标旨在与全部"构建相同的项目,除了使用一些不同的编译开关(例如,-ggdb).由于目标使用不同的编译开关,因此,如果您在两者之间进行切换,则显然需要重建整个项目.但是GNUmake并不自然地意识到这一点.

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 compile switches (like -ggdb, for example). Since the targets use different compile switches, you obviously need to rebuild the entire project if you switch between the two. But GNUmake doesn't naturally recognize this.

因此,如果您键入make all,您将会得到

So if you type make all you'll get

Building ...
...

然后,如果您键入make debug,您将得到

Then if you type make debug, you get

make: Nothing to be done for `debug'.

所以我的问题是:您如何在Makefile中实现一个干净的解决方案,以注意上一个版本使用的伪目标或编译开关不同于您当前想要的伪目标或编译开关?如果它们不同,Makefile将重建所有内容.

So my question is: how do you implement a clean solution in the Makefile to notice that the last build used a different pseudo-target, or different compile switches, than the one you want currently? If they are different, the Makefile would rebuild everything.

推荐答案

将构建产品放入不同的目录树中(当然要保留一份源代码副本).这样,您始终只是从最新版本进行的简短编译,无论是调试还是发布(甚至其他).也不可能造成混乱.

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.

编辑

上面的草图.

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

(免责声明:未经测试,甚至没有运行过make.)

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

你去了. -j甚至是安全的,因此您可以执行make -j5 all debug.有很多明显的样板只是为了整理而哭泣.

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.

这篇关于如何实现一个记住最后一个构建目标的Makefile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:46