【问题标题】:Parent target name of the current target in GNU MakefileGNU Makefile 中当前目标的父目标名称
【发布时间】:2018-12-11 10:40:52
【问题描述】:

我需要什么:

all: release debug

release: compile

debug: compile

compile: 
    if parent_target_name = release: 
        $(CXXFLAGS) = for rel
    else: $(CXXFLAGS) = for deb

问题: 如何查看调用当前目标的目标名称?

我已经看到这个问题GNU Make get parent target name,但它没有帮助。

【问题讨论】:

  • 您可能正在寻找的是Target-specific Variable Values。如果您仔细阅读手册的这一部分,您将了解它们如何传播到先决条件。
  • 我已阅读此部分。据我了解,特定于目标的变量仅使我能够在本地更改全局变量。

标签: makefile gnu


【解决方案1】:

您可能正在寻找的是Target-specific Variable Values。如果您仔细阅读手册的这一部分,您会看到它们如何传播到先决条件。

只是为了说明它们是如何工作的:

.PHONY: all release debug compile

all:
    $(MAKE) release
    $(MAKE) debug

release: CXXFLAGS = for rel
debug: CXXFLAGS = for deb

release debug: compile
    @echo 'building $@ with CXXFLAGS = $(CXXFLAGS)'

compile: a b c
    @echo 'building $@ with CXXFLAGS = $(CXXFLAGS)'

a b c:
    @echo 'building $@ with CXXFLAGS = $(CXXFLAGS)'

演示:

$ make --no-print-directory all
make release
building a with CXXFLAGS = for rel
building b with CXXFLAGS = for rel
building c with CXXFLAGS = for rel
building compile with CXXFLAGS = for rel
building release with CXXFLAGS = for rel
make debug
building a with CXXFLAGS = for deb
building b with CXXFLAGS = for deb
building c with CXXFLAGS = for deb
building compile with CXXFLAGS = for deb
building debug with CXXFLAGS = for deb

【讨论】:

  • 谢谢!)非常完美
  • 请注意,此示例有效,因为所有目标都是虚假的(无论是否明确)。它们总是被重建。如果(很可能)您的最终目标是文件,您将遇到一个新问题:一旦构建了文件,make 将不会仅仅因为标志更改而重新构建它...
  • 好的。还有一个问题:如何在此处添加新变量release: CXXFLAGS = for rel
  • 如果您想定义新的目标特定变量,只需为每个目标/变量对添加一个新的target: variable = value 行。
  • @SergeiShumilin:“如何让a 每次都启动”。 make clean 介于 make debugmake release 之间,或者针对不同情况有不同的目标(例如 a_debuga_release)。这些不同的目标可以有相同的来源但不同的规则。例如在this recent question and its answers
猜你喜欢
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 1970-01-01
相关资源
最近更新 更多