当先决条件没有后缀或前缀时,make 无法确定隐式规则?
make can't determine implicit rules when prerequisite has no suffix or prefix?
我有一个简单的 make 文件,我只想将每个 .c 文件放在一个文件夹中并分别编译它们,然后 运行 它们将输出转储到具有相同文件名的文本文件中。
%.txt: %
./$< > $@
例如,如果我有 p1.c
,则有一个隐式规则,我可以从 p1.c
生成 p1
可执行文件,然后我的规则指定您可以生成 p1.txt
通过执行 p1
但这似乎不起作用:
$ make p1.txt
make: *** No rule to make target `p1.txt'. Stop.
但是我可以显式制作可执行文件然后制作文本文件(因为隐式规则在只有 1 步时起作用,而文本文件在可执行文件已经存在时起作用)
$ make p1
cc p1.c -o p1
$ make p1.txt
./p1 > p1.txt
如果我也在 makefile 中明确命名文件,一切正常:
%.txt: %
./$< > $@
p1.txt: p1
这适用于 p1
但我希望它只适用于我文件夹中的所有 c 文件并编写某种形式的 for each
只是为了让 make 明确地看到依赖关系似乎很愚蠢。
查看 the documentation for implicit rule lookup 并根据我的理解,当文件中明确提及依赖项时,第 5 步确定:
(If a file name is mentioned in the makefile as a target or as an explicit dependency, then we say it ought to exist.)
If all dependencies exist or ought to exist, or there are no dependencies, then this rule applies.
当它是明确的 p1.txt: p1
意味着依赖性 p1
应该存在并且存在一个隐含的规则来这样做。当它没有明确存在时,我唯一不确定的部分是“终端”在步骤 6.1 中的含义
If the rule is terminal, ignore it and go on to the next rule
我需要指定什么才能使用从 .c 文件生成的可执行文件作为依赖项?
如果这恰好是版本特定的错误,我正在使用 GNU Make 3.81
。
如果您访问 GNU 官方网站并阅读 the manual there, you'll see that it has an extensive index. If you look through that index to find "terminal rule", it will send you to the section on Using Implicit Rules。
这会让您明白您的规则不是最终规则(因为它们不使用双冒号定义)。但是 make 会特别对待 match-anything 规则(目标只是 %
的规则,内置规则就是),这意味着您的尝试无法奏效。
我有一个简单的 make 文件,我只想将每个 .c 文件放在一个文件夹中并分别编译它们,然后 运行 它们将输出转储到具有相同文件名的文本文件中。
%.txt: %
./$< > $@
例如,如果我有 p1.c
,则有一个隐式规则,我可以从 p1.c
生成 p1
可执行文件,然后我的规则指定您可以生成 p1.txt
通过执行 p1
但这似乎不起作用:
$ make p1.txt
make: *** No rule to make target `p1.txt'. Stop.
但是我可以显式制作可执行文件然后制作文本文件(因为隐式规则在只有 1 步时起作用,而文本文件在可执行文件已经存在时起作用)
$ make p1
cc p1.c -o p1
$ make p1.txt
./p1 > p1.txt
如果我也在 makefile 中明确命名文件,一切正常:
%.txt: %
./$< > $@
p1.txt: p1
这适用于 p1
但我希望它只适用于我文件夹中的所有 c 文件并编写某种形式的 for each
只是为了让 make 明确地看到依赖关系似乎很愚蠢。
查看 the documentation for implicit rule lookup 并根据我的理解,当文件中明确提及依赖项时,第 5 步确定:
(If a file name is mentioned in the makefile as a target or as an explicit dependency, then we say it ought to exist.) If all dependencies exist or ought to exist, or there are no dependencies, then this rule applies.
当它是明确的 p1.txt: p1
意味着依赖性 p1
应该存在并且存在一个隐含的规则来这样做。当它没有明确存在时,我唯一不确定的部分是“终端”在步骤 6.1 中的含义
If the rule is terminal, ignore it and go on to the next rule
我需要指定什么才能使用从 .c 文件生成的可执行文件作为依赖项?
如果这恰好是版本特定的错误,我正在使用 GNU Make 3.81
。
如果您访问 GNU 官方网站并阅读 the manual there, you'll see that it has an extensive index. If you look through that index to find "terminal rule", it will send you to the section on Using Implicit Rules。
这会让您明白您的规则不是最终规则(因为它们不使用双冒号定义)。但是 make 会特别对待 match-anything 规则(目标只是 %
的规则,内置规则就是),这意味着您的尝试无法奏效。