首页 > 解决方案 > 如何使用 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 中是如何工作的?

标签: makefilewildcardgnu-makegnu

解决方案


如果您不回避使用帮助代码(即包含一个GNUmake库),那么GNUmake 表工具包肯定可以解决问题:

include gmtt.mk

xyz:= abc_def

$(xyz):
   (some commands here which generates a file)

   mv file /tmp/$(firstword $(call glob-match,$@,*_*)).cpp 

glob-match函数将字符串拆分为匹配元素的条纹,其中每个全局字符(*, ?, [...])和逐字字符串部分(在您的情况下只是_)构成一个匹配项。或者简单地说,$(call glob-match,this_is_a_string,*_is_a_*)分成this_is_a_string一个列表this _is_a_ string(注意空格)。


推荐阅读