首页 > 解决方案 > GNU Makefile 中当前目标的父目标名称

问题描述

我需要的:

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但它没有帮助。

标签: makefilegnu

解决方案


您可能正在寻找的是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

推荐阅读