【问题标题】:patsubst's returned value when no pattern found未找到模式时 patsubst 的返回值
【发布时间】:2018-07-05 20:43:31
【问题描述】:

我正在尝试将“sub-make”命令发送到子目录。我有一个带有“top_level” Makefile 的父目录和一个带有自己的 Makefile 的子目录。

我的父 Makefile 有一个目标行如下:

target%:
    make -C sub_dir $(patsubst target-%,%,$@)

我可以在父文件夹中做:

使目标干净 && 使目标全部

它将被解释为:

make -C sub_dir clean && make -C sub_dir all

我希望能够做到:

制作目标

但在这种情况下,我得到:

make -C sub_dir target.o

我期待虽然“patsubst”没有找到模式,但它不会返回任何内容或测试的表达式。但它返回这个“target.o”。

谁能给我解释一下?我怎么可能一无所获?

我尝试了这些表达式但没有成功:

make -C sub_dir $(patsubst target%,%,$@)
make -C sub_dir $(patsubst -%,%,$(patsubst target%,%,$@))
make -C sub_dir $($(patsubst -%,%,$(patsubst target%,%,$@)):.o=)

最后一个很棘手,它给出了:

make -C sub_dir
make[1]: 进入目录/home/aurelien/Documents/Projects/parent/sub_dir'
make[1]: 'subtarget' 是最新的。
make[1]: 离开目录 '/home/aurelien/Documents/Projects/parent/sub_dir'
cc 目标.o -o 目标
cc: target.o: 没有这样的文件或目录
cc: 没有输入文件
make: *** [目标] 错误 1 ​​

【问题讨论】:

    标签: makefile


    【解决方案1】:

    target% 模式匹配但至少与 % 匹配一个字符,而不是零。来自 GNU make 手册:

    模式规则在 目标;否则,它看起来就像一个普通的规则。目标 是匹配文件名的模式; '%' 匹配任何非空 子字符串,而其他字符只匹配自己。

    所以target 不匹配,make 使用其隐式规则构建target:构建target.o,然后从target.o 构建target。当试图找到构建target.o 的方法时,模式规则匹配并且配方:

    make -C sub_dir $(patsubst target-%,%,$@)
    

    展开。但是由于target.otarget-% 不匹配,patsubst 什么都不做,你的食谱变成:

    make -C sub_dir target.o
    

    【讨论】:

      【解决方案2】:

      问题是模式必须匹配一个或多个字符。所以,这个模式:

      target%:
      

      与目标名称target 不匹配,因为没有剩余的字符可以匹配模式。

      所以,make 寻找另一个可用于构建 target 的隐式规则并找到内置规则:

      % : %.o
      

      所以它尝试构建target.o这个目标匹配模式target%.o,所以它会尝试使用你的规则来构建它。

      为了具体回答您的问题,任何与patsubst 中的替换模式不匹配的单词都将被传递而不更改。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-28
        • 2016-11-23
        • 2021-05-15
        • 1970-01-01
        相关资源
        最近更新 更多