gpt4 book ai didi

Makefile:配方中模式规则%的重用值

转载 作者:行者123 更新时间:2023-12-04 22:43:07 25 4
gpt4 key购买 nike

在Makefile中,我有:

images/schematic.pdf: images/schematic.svg

  inkscape -D -z --file=$^ --export-pdf=$@ --export-latex

sed -i "s:schematic:images/schematic:g" $@_tex

该规则的作用是:
  • 使用inkscape转换为可用于 latex 的.pdf PDF文件及其相应的.pdf_tex文本文件(请参见此答案:https://tex.stackexchange.com/a/2107/104581)
  • 修改提到的.pdf_tex文件,以使它不会破坏从“较高”目录(即.文件位于.pdf_tex中时为./images)进行的 latex 编译。

  • 我的问题:
    我有许多这种形式的规则。 e。仅更改 schematic的位置。我想使用 pattern-ruleschematic替换为 %。并在配方中使用 %(在sed命令中)。
    但是规则:

    images/%.pdf: images/%.svg

      inkscape -D -z --file=$^ --export-pdf=$@ --export-latex

    sed -i "s:%:images/%:g" $@_tex

    不起作用:%在配方中按字面意义进行解释。
    我也尝试用$%代替配方中的%,但是此变量似乎为空。
    不能令人满意的解决方案:
    在配方中添加一行以创建一个(make)变量,该变量将保存 notdir(removeprefix($<))的结果(使用 this question或对bash的调用,因为GNU Make中没有removeprefix)。

    最佳答案

    您需要$*
    https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html:

    $*:The stem with which an implicit rule matches (see How Patterns Match).If the target is dir/a.foo.b and the target pattern is a.%.bthen the stem is dir/foo. The stem is useful for constructing names ofrelated files.

    In a static pattern rule, the stem is part of the file name that matched the ‘%’ in the target pattern.

    In an explicit rule, there is no stem; so ‘$*’ cannot be determined in that way. Instead, if the target name ends with arecognized suffix (see Old-Fashioned Suffix Rules), ‘$*’ is set to thetarget name minus the suffix. For example, if the target name is‘foo.c’, then ‘$*’ is set to ‘foo’, since ‘.c’ is a suffix. GNU makedoes this bizarre thing only for compatibility with otherimplementations of make. You should generally avoid using ‘$*’ exceptin implicit rules or static pattern rules.

    If the target name in an explicit rule does not end with a recognized suffix, ‘$*’ is set to the empty string for that rule.

    $%是一个自动变量,但它是针对存档( .a库)成员的,几乎从不使用。

    关于Makefile:配方中模式规则%的重用值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39604452/

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