Make:在静态模式规则中设置目标特定的变量 [英] Make: Setting target-specific variables in static pattern rules

查看:185
本文介绍了Make:在静态模式规则中设置目标特定的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用静态模式规则编写Makefile,并且我希望为TARGETS的每个元素分配一个分配给当前目标名称(此处为词干'%')的变量.

I'm writing a Makefile using static pattern rules and I want for each element of TARGETS a variable assigned to the current target name (here the stem '%').

TARGETS = a b c d

all : $(TARGETS)
$(TARGETS) : % : DIR = %
$(TARGETS) : % : %_setup build

a_setup :
 code for a
b_setup :
 code for b
...
build
 code using "DIR = XX" previously configured

但是gnumake抱怨目标特定的变量DIR:

but gnumake complains about the target-specific variable DIR:

make: *** No rule to make target 'DIR', needed by 'a'

是否可以混合使用静态模式规则和变量分配?谢谢!

Is it possible to mix static pattern rules and variable assignation? Thanks!

推荐答案

根据GNU make手册,您不能那样做.但是,您可以使用$@.在您的示例中,您可以直接分配DIR=$@,但更一般地,您可以将$@patsubst结合使用:

According to the GNU make manual you can't do it like that. However, you can use $@. In you example you can directly assign DIR=$@ but more generally you can use $@ in combination with patsubst:

TARGETS = a b c d
all : $(TARGETS)
$(TARGETS) : DIR = $(patsubst %,%,$@)
$(TARGETS) : % : %_setup build
        echo $@: DIR:$(DIR)
%_setup :
        echo $@
build:
        echo $@

这篇关于Make:在静态模式规则中设置目标特定的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆