gpt4 book ai didi

makefile - 如何在 GNU Make 模式规则中包含路径前缀

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

考虑以下:

%.foo: %.bar
echo $< > $@

假设我们有一个文件 1.bar ,执行的命令很简单 echo 1.bar > 1.foo .然而,当 %包含一个路径,而不仅仅是一个文件名,它开始变得挑剔。我的问题是我想为 %.bar 添加另一条路径,模式变得完全困惑。即,当 %.barnice/path/1.bar ,这变得不可能:
%.foo: /project/something/%.bar
echo $< > $@

这将运行,但它执行 echo nice/path//project/something/1.bar > 1.foo代替 echo /project/something/nice/path1.bar > 1.foo
原因在于 make 如何执行其模式规则。从文档:

When the target pattern does not contain a slash (and it usually does not), directory names in the file names are removed from the file name before it is compared with the target prefix and suffix. [...] The directories are ignored only for the purpose of finding an implicit rule to use, not in the application of that rule. Thus, ‘e%t’ matches the file name src/eat, with ‘src/a’ as the stem. When prerequisites are turned into file names, the directories from the stem are added at the front, while the rest of the stem is substituted for the ‘%’. The stem ‘src/a’ with a prerequisite pattern ‘c%r’ gives the file name src/car



有什么办法可以针对特定规则关闭此功能吗?

最佳答案

您可能想阅读 How Patterns Match :

When the target pattern does not contain a slash (and it usually does not), directory names in the file names are removed from the file name before it is compared with the target prefix and suffix. After the comparison of the file name to the target pattern, the directory names, along with the slash that ends them, are added on to the prerequisite file names generated from the pattern rule’s prerequisite patterns and the file name. The directories are ignored only for the purpose of finding an implicit rule to use, not in the application of that rule. Thus, ‘e%t’ matches the file name src/eat, with ‘src/a’ as the stem. When prerequisites are turned into file names, the directories from the stem are added at the front, while the rest of the stem is substituted for the ‘%’. The stem ‘src/a’ with a prerequisite pattern ‘c%r’ gives the file name src/car.



以上解释了为什么 nice/path/前置于先决条件 /project/something/1.bar .

一种解决方法是在规则中使用完整的文件名,例如:
${CURDIR}/nice/path/%.foo: /project/something/%.bar    

关于makefile - 如何在 GNU Make 模式规则中包含路径前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29799855/

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