如何正确逃避“%"在GNU make中使用模式规则和patsubst时是否要签名? [英] How to correctly escape "%" sign when using pattern rules and patsubst in GNU make?

查看:62
本文介绍了如何正确逃避“%"在GNU make中使用模式规则和patsubst时是否要签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似以下的makefile:

I have a makefile like the following:

m1:
    @echo building m1

m1_:
    @echo building m1_


m2:
    @echo building m2

m2_:
    @echo building m2_

m3_DEPS = m2 m1

SUBSTITUTE=$(patsubst %,%_,$($@_DEPS))
.SECONDEXPANSION:

#%: $$(SUBSTITUTE)
%:  $$(patsubst \%,\%_,$$($$@_DEPS))
    @echo Building $@
    @echo Dependencies are $^

关键所在是

%:  $$(patsubst \%,\%_,$$($$@_DEPS))

我同时使用模式规则和patsubst,后者本身使用百分号.我以为可以用\转义%字符,但是我仍然没有得到预期的行为.运行"make m3"给出输出

I am using both a pattern rule and patsubst, which itself uses percentage signs. I thought I could escape the % character with a \, but I am still not getting the expected behaviour. Running "make m3" gives the output

building m2
building m1
Building m3
Dependencies are m2 m1

但是,我希望得到

building m2_
building m1_
Building m3
Dependencies are m2_ m1_

注释该行并通过变量间接调用patsubst实际上会产生该输出.

Commenting out that line and calling patsubst indirectly through a variable does in fact produce that output.

SUBSTITUTE=$(patsubst %,%_,$($@_DEPS))

%: $$(SUBSTITUTE)

我还测试了使用非模式规则的有效性,这使我认为这与模式规则和百分号的相互作用有关:

Also, I have tested that using a non-pattern rule works, which makes me think it is something to do with the interaction of pattern rules and percentage signs:

m3:  $$(patsubst %,%_,$$($$@_DEPS))

Makefile上下文中的

推荐答案

\用于行继续,而不用于转义".要转义,可以将它们隐藏在变量中:

\ in makefile context is for line continuation, not for "escaping". To escape things you hide them in a variable:

PERCENT := %

这个想法是,在解析makefile片段时,如果转义的字符有意义,则可以对其进行转义.

The idea is, at the time a makefile fragment is parsed where the escaped character is meaningful, you escape it.

因此,根据您的情况,您必须使用$$(PERCENT):

So, in your situation, you have to use $$(PERCENT):

$$(patsubst $$(PERCENT),$$(PERCENT)_,$$($$@_DEPS))

这篇关于如何正确逃避“%"在GNU make中使用模式规则和patsubst时是否要签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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