【问题标题】:How to define a variable and then compile in a target [Makefile]如何定义变量然后在目标中编译 [Makefile]
【发布时间】:2015-11-03 22:20:16
【问题描述】:

有一个 Makefile 我只想调整一个变量然后编译,如果我调用一个特定的目标。我有一个可行的解决方案,但是,它不是一个好的解决方案。

我是如何编译的:

make

我想如何更改变量:

make debug

需要发生什么(它是如何工作的):

debug:
    @make TAG=debug

我基本上在 make 过程中调用 make,它可以完成工作,但似乎也不正确。我正在寻找类似的东西:

debug:
    TAG=debug
    jump to first line and do the job

【问题讨论】:

  • 自己先运行make TAG=debug
  • 因为没有自动补全功能,而且我也喜欢 debug-clean 和 debug-distclean ....
  • 嗯,通常的方式是make BUILD_TYPE=Debug

标签: c++ makefile


【解决方案1】:
TAG=release

build:
    @echo $(TAG)

debug: TAG=debug
debug: build

release: build

用法:

> make
release
> make release
release
> make debug
debug

更新: 在构建先决条件中使用TAG

TAG=release

pre:
    @echo pre build $(TAG)

build: pre
    @echo $(TAG)

debug: TAG=debug
debug: build

release: build

输出:

~/workspace (master) $ make build
pre release
release
~/workspace (master) $ make debug
pre debug
debug
~/workspace (master) $ make release
pre release
release

【讨论】:

  • 只要在任何其他目标的先决条件列表中不需要 TAG 的值,它就可以工作。
  • @EtanReisner 你能解释一下吗? TAG 在我的示例中默认设置为 release
  • 是的,该值将在先决条件中起作用。尝试使用TAG=debug 并在build 的先决条件下使用$(TAG)。 (请参阅this question 上的 cmets,了解我对此进行的讨论,尽管此时我的部分已被删除。)
  • 实际上this question 涵盖了该问题和解决方法。
  • @EtanReisner 我不明白你想说什么。我更新了我的答案并向build 添加了先决条件。为我工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多