makefile 中的百分号有什么作用?

发布于 2024-12-04 03:50:48 字数 325 浏览 1 评论 0原文

我有一个如下所示的 makefile:

include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS))

%-test: 
       Something here

我了解它在目标规则行中的用途。第一行中的 % 符号是做什么的?它与目标规则行中的百分号有什么关系吗?

当我编写 make sometarget 时,makefile 中未作为任何规则的一部分编写的行(例如此 makefile 中的第一行)是否会执行?如果是,那么执行顺序是什么?

I have a makefile that looks like this :

include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS))

%-test: 
       Something here

I understand what it is intended for in the target rule line. What is the % sign doing in the first line ? Does it have anything to do percent sign in the target rule line ?

When I write make sometarget, are the lines in the makefile that are not written as part of any rule (like the first line in this makefile) executed at all ? If yes, then what is the order of execution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2024-12-11 03:50:48

正如您在 GNU make 手册中所读到的,百分比充当通配符。 patsubst 函数 的第一个参数构成图案。最后一个参数中的每个项目/单词都会与该模式进行比较,如果匹配,则将其替换为第二个参数。如果模式中有通配符 (%),则这将匹配任意数量的字符,并且这些字符将被复制到替换字符串中第二个参数中 % 的位置。

在您的示例中,模式只是通配符,因此它将匹配函数最后一个参数中的任何单词,并且该单词将被复制到 % 位置的替换字符串(第二个参数)中。

一个例子可能会让事情变得更清楚。假设 TEST_SUBDIRS 包含两个名称。

TEST_SUBDIRS := test1 test2
include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS))

这相当于以下内容。

include $(src)/test1/Make.tests $(src)/test2/Make.tests

生成文件按顺序逐行处理。变量赋值是“内部化的”,并且 include 语句会导致其他文件的内容按字面意思插入到该位置,然后该内容将作为 makefile 的一部分进行处理。
读入规则时会形成依赖关系图,处理整个文件后,将执行必要的配方以更新请求的目标。

As you can read in the GNU make manual, the percent acts as a wildcard. The first argument of the patsubst function forms the pattern. Each item/word in the last argument is compared against this pattern, and if it matches, it is replaced with the second argument. If there is a wildcard symbol (%) in the pattern, this will match any number of characters, and these characters are copied into the replacement string at the place of the % in the second argument.

In your example the pattern is just the wildcard symbol, so it will match any word in the last argument to the function, and this word will be copied into the replacement string (the second argument) at the place of the %.

An example may make things more clear. Let's assume TEST_SUBDIRS contains two names.

TEST_SUBDIRS := test1 test2
include $(patsubst %,$(src)/%/Make.tests, $(TEST_SUBDIRS))

This is then equivalent to the following.

include $(src)/test1/Make.tests $(src)/test2/Make.tests

A makefile is processed sequentially, line by line. Variable assignments are "internalized", and include statements cause the contents of other files to be inserted literally at that location after which that content is processed as part of the makefile.
A dependency graph is formed from the rules as they are being read in, and after the entire file is processed, the necessary recipes are executed to update the requested target.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文