gpt4 book ai didi

makefile - 如何在仅限订单的先决条件内调用函数?

转载 作者:行者123 更新时间:2023-12-04 20:18:25 26 4
gpt4 key购买 nike

鉴于这一点 Makefile:

# for pattern matching
$(OBJDIR) := build

# just to see if a level of indirection will work
my_dir = $(dir $(1))

$(OBJECTS) : $(OBJDIR)/% : $(HEADERS) $(SRCDIR)/% | % $(dir %) $(call my_dir,%)
@echo output-only = $|

这是 static pattern ruleorder-only prerequisites .

考虑目标“build/utility/debug.js”。上述规则的输出将是这样的:
output-only = utility/debug.js ./
  • 第一个组件“utility/debug.js”是从词干 (%) 正确复制的。
  • 第二个组件“./”是调用 dir 的输出。先决条件列表中的函数。
  • 第三个组件,一个空字符串,是调用我的 my_dir 的输出。先决条件列表中的函数。

  • 如果我改变 my_dir对此:
    my_dir = $(1)

    输出保持不变。如果我将其更改为:
    my_dir = "foo"

    然后make提示没有规则可以制作“foo”(这是预期的)。看来, $(1)未绑定(bind)到 my_dir 的调用.

    这是怎么回事?为什么我不能将词干传递给函数?我有一个使用二次扩展的解决方法,但我想知道为什么我不能这样写我的规则。

    编辑:我是stackoverflow的新手,如果这不是这里的处理方式,请原谅我。

    我想要 $(1) 因为我将词干作为参数传递给 my_dir ,正如亚历克斯指出的那样。

    我不知道为什么有人建议我想要“$”。我不相信 $ 本身在任何情况下都可以扩展到任何东西。

    我知道自动变量仅在配方中可用。我没有在先决条件中使用自动变量 - 我使用的是词干:

    Each target is matched against the target-pattern to extract a part of the target name, called the stem. This stem is substituted into each of the prereq-patterns to make the prerequisite names (one from each prereq-pattern). - the manual



    示例证明了茎可用的事实:茎在单独使用时扩展为正确的值,但在传递给函数时不会。

    最佳答案

    可以在 this section of the GNU make manual 中看到,先决条件列表中的变量和函数引用在读入阶段立即展开。这意味着,在完成任何模式匹配之前,%尚无特殊意义;它在两个函数引用 $(dir %) 中被解释为文字字符。和 $(call my_dir,%) , 两者都有 ./结果,它们被合并到对 $| 的引用中在食谱中。

    除了您已经找到的解决方法之外,我不知道任何其他解决方法,即二次扩展。

    关于makefile - 如何在仅限订单的先决条件内调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526295/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com