Make模式规则中包含路径前缀

Make模式规则中包含路径前缀

本文介绍了如何在GNU Make模式规则中包含路径前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

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

假设我们只有一个文件1.bar,执行的命令就是echo 1.bar > 1.foo.但是,当%包含路径而不是仅文件名时,它开始变得挑剔.我的问题是我想在%.bar之前添加另一条路径,该模式会完全混乱.即,当%.barnice/path/1.bar时,这变得不可能:

Assuming we have one file 1.bar, the command executed is simply echo 1.bar > 1.foo. However, when % contains a path, rather than just a file name, it start becoming finicky. My problem is that I want to prepend another path to %.bar, the pattern becomes completely mangled. I.e., when %.bar is nice/path/1.bar, this becomes impossible:

%.foo: /project/something/%.bar
    echo $< > $@

这将运行,但它将代替echo /project/something/nice/path1.bar > 1.foo执行echo nice/path//project/something/1.bar > 1.foo

This will run, but it executes echo nice/path//project/something/1.bar > 1.foo in stead of echo /project/something/nice/path1.bar > 1.foo

这样做的原因在于其模式规则的制作方式.从文档中:

The reason for this is in how make does its pattern rules. From the docs:

有什么方法可以针对特定规则将其关闭吗?

Is there any way I can turn this off for a specific rule?

推荐答案

您可能想阅读模式如何匹配:

以上内容解释了为什么nice/path/被置于前提条件/project/something/1.bar之前.

The above explains why nice/path/ is prepended to prerequisite /project/something/1.bar.

一种解决方法是在规则中使用完整文件名,例如:

One fix would be to use full file names in rules, e.g.:

${CURDIR}/nice/path/%.foo: /project/something/%.bar

这篇关于如何在GNU Make模式规则中包含路径前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:12