如何解决如何使用 patsubst Makefile 替换?
我有一个类似这样的 makefile。 我需要生成一个文件并将其移动为 abc.cpp(基本上去掉下划线后的任何内容,包括下划线
xyz:= abc_def
$(xyz):
(some commands here which generates a file)
mv file /tmp/$(patsubst _%,"",$@)
However this does not work. In fact it doesn't ever match the underscore "_" in $@
mv file /tmp/abc.cpp is what i want
“%”通配符在 patsusbst 中如何工作?
解决方法
patsubst
函数对您不起作用,因为它只能匹配一种模式。您想要匹配两种模式:_
之前的任何内容和 _
之后的任何内容。 $(patsubst _%,...)
只匹配开头以_
开头的词,而你的词abc_def
不以_
开头,所以patsubst
无操作。
要使用 GNU make 函数做您想做的事,您需要玩个把戏;类似:
mv file /tmp/$(firstword $(subst _,$@))
通过将 _
更改为空格,将字符串拆分为单词,然后取第一个单词。
如果您不回避使用辅助代码(即包含 GNUmake
库),那么 the GNUmake table toolkit 肯定可以解决问题:
include gmtt.mk
xyz:= abc_def
$(xyz):
(some commands here which generates a file)
mv file /tmp/$(firstword $(call glob-match,$@,*_*)).cpp
glob-match
函数将字符串拆分为匹配元素的条纹,其中每个 glob 字符 (*
,?
,[...]
) 和逐字字符串部分(在您的情况下)仅 _
) 构成一场比赛。或者简单地说,$(call glob-match,this_is_a_string,*_is_a_*)
将 this_is_a_string
拆分为列表 this _is_a_ string
(注意空格)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。