【问题标题】:How to replace using patsubst Makefile?如何使用 patsubst Makefile 替换?
【发布时间】:2021-01-13 23:15:59
【问题描述】:

我有一个有点像这样的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 中如何工作?

【问题讨论】:

    标签: makefile wildcard gnu-make gnu


    【解决方案1】:

    patsubst 函数对你不起作用,因为它只能匹配一个模式。您想要匹配两种模式:_ 之前的任何内容和_ 之后的任何内容。 $(patsubst _%,...) 只匹配_ 开头 的单词,而你的单词abc_def 不以_ 开头,因此patsubst 是无操作的。

    要使用 GNU make 函数做你想做的事,你需要耍花招;类似:

    mv file /tmp/$(firstword $(subst _, ,$@))
    

    这通过将_ 更改为空格将字符串拆分为单词,然后取第一个单词。

    【讨论】:

      【解决方案2】:

      如果您不回避使用帮助代码(即包含 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 函数将字符串拆分为匹配元素的条纹,其中每个全局字符(*?[...])和逐字字符串部分(在您的情况下只是 _)构成一个匹配。或者简单地说,$(call glob-match,this_is_a_string,*_is_a_*)this_is_a_string 拆分成一个列表this _is_a_ string(注意空格)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 2020-03-25
        • 2017-12-19
        相关资源
        最近更新 更多