【问题标题】:Understanding pattern rules了解模式规则
【发布时间】:2014-11-17 18:47:57
【问题描述】:

我正试图围绕模式规则以及它们的功能展开思考。我正在使用this文章作为参考,其中说明了

模式规则是为许多文件指定规则的简洁方式 一次。该规则将取决于文件名,但通常取决于 以简单的方式在他们身上。您可以使用 % 指定模式 通配符。当存在于依赖列表中时, % 匹配任何字符串 任何长度;当出现在目标列表中时,% 代表 依赖列表中 % 匹配的字符串。

以下模式规则将获取任何 .c 文件并将其编译为 .o 文件:

%.o: %.c $(CC) $(CFLAGS) $(INCLUDES) -c $(input) -o $(output)

(假设您有变量 CC、CFLAGS 和 INCLUDES 定义为合适的东西。 Makepp 会猜测 CC 的值,然后 CFLAGS。)

规则的第一行说它适用于所有可能的情况 匹配模式 %.c 的输入文件。这些 .c 文件可以是 使用指定的转换成对应的 .o 文件 行动。

规则的动作与我们见过的其他动作非常相似 以前,除了它使用自动变量。一个自动 variable 是一个变量,其值由 makepp 自动设置 取决于它出现的规则。一些有用的自动 变量是:

$(input)
    The name of the first input file. In this rule, this would be the file that matches the %.c pattern. $(dependency) is a synonymn for $(input). In older makefiles, you will also see the cryptic symbol $< used as well. 
$(output)
    The name of the first output file. In this rule, this would be the file that matches the %.o pattern. $(target) and $@ are synonymns. 
$(inputs)
    The name of all explicitly listed input files. In this case, since there is only one, $(inputs) is equivalent to $(input). $(dependencies) and $^ are synonymns. 
$(outputs)
    The name of all explicitly listed targets. In this case, since there is only one, $(outputs) is equivalent to $(output). $(targets) is a synonymn for $(outputs). 

以下是我的问题:

1) 假设我有 2 个文件 FileA.c 和 FileB.c。当我应用上述模式规则时,它将如何应用于上述两个文件。给出的示例仅处理一个文件。

2) 自动变量inputinputs有什么区别

【问题讨论】:

    标签: makefile


    【解决方案1】:

    一个模式规则将被应用到每个与 make 需要构建的规则相匹配的目标文件。

    因此,如果您需要同时构建 FileA.oFileB.o(因为它们都被列为某些其他目标(例如 FileBin: FileA.o FileB.o)的先决条件),则该规则将运行两次,每次运行一次。

    遵守规则

    FileBin: FileA.o FileB.o
            @echo '$$input = $(input)'
            @echo '$$inputs = $(inputs)'
    

    运行时会输出

    $input = FileA.o
    $inputs = FileA.o FileB.o
    

    还应该指出,inputoutput 是 makepp 变量名,对 GNU make 本身无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      相关资源
      最近更新 更多