对一个目标进行回显

发布于 2025-01-02 16:39:07 字数 447 浏览 1 评论 0原文

我有一个 makefile,它在目录中查找 .txt 文件,并为每个文件生成其名称的回显。

pchs := $(wildcard $(OUTPUT:%=%/*.txt))
txt:  $(pchs)
%.txt:
    echo $@

但是当我启动它时,make 实用程序返回我不需要对 txt 执行任何操作。为什么?

编辑1:

经过一些回答后,我明白了我应该用我的makefile做什么。现在看起来像这样:

pchs := $(wildcard $(OUTPUT:%=%/*.txt))

.PHONY : $(pchs)

txt:  $(pchs)

%.txt:
    @echo pch is '$<'

但是.PHONY 并没有帮助我,制作的结果是相同的。

I have a makefile which looks for .txt files in a directory and for each file makes echo of it name.

pchs := $(wildcard $(OUTPUT:%=%/*.txt))
txt:  $(pchs)
%.txt:
    echo $@

But when I start it the make utility returns me that nothing to be done for txt. Why?

EDIT1:

After some answers I understand what I should make with my makefile. Now it looks like this:

pchs := $(wildcard $(OUTPUT:%=%/*.txt))

.PHONY : $(pchs)

txt:  $(pchs)

%.txt:
    @echo pch is '
lt;'

But .PHONY does not help me the result of making is the same.

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

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

发布评论

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

评论(4

沐歌 2025-01-09 16:39:07

为什么 make 说没有什么可做的?因为 make 计算目标的依赖关系,通常是文件目标。并且“txt”目标不会生成任何文件。

.PHONY 适用于不生成文件的目标,例如干净目标。

这里应该有效:

pchs := $(wildcard $(OUTPUT:%=%/*.txt))

.PHONY: txt

txt: $(pchs)
    echo $@

但是,由于您只回显文件名,我猜您正在后期处理此输出。也许您可以在 makefile 中将此后处理制定为规则?

Why does make says, that there ist nothing to do? Because make calculates dependencies of targets, usually file targets. And the "txt" target produces no file.

.PHONY is for targets, that produce no file, like the clean target.

This here should work:

pchs := $(wildcard $(OUTPUT:%=%/*.txt))

.PHONY: txt

txt: $(pchs)
    echo $@

But, since you only echo the filename, I guess that you are post processing this output. Maybe you could formulate this post processing as a rule in the makefile?

〃安静 2025-01-09 16:39:07

因为 makefile 定义了您想要构建的内容。并且 .txt 文件已经存在,因此无需执行任何操作。

要解决这个问题,有多种可能性,但如果至少使用 gnu-make,您应该查看 .PHONY 记录。

您可以从 txt 记录中构建虚假内容并将其标记为虚假内容。但是......这样做可能更容易:

pchs := $(wildcard $(OUTPUT:%=%/*.txt))
txt:
    for i in $(pchs) ; do echo $i ; done

Because makefiles define what you want to have built. And the .txt files already exist, so there is nothing to do.

To solve this there are a number of possibilities, but you should look into the .PHONY record if using gnu-make at least.

You can build fake-things out of the txt records and mark them as phony. But... it might just be easier to do this:

pchs := $(wildcard $(OUTPUT:%=%/*.txt))
txt:
    for i in $(pchs) ; do echo $i ; done
手心的海 2025-01-09 16:39:07

这是因为您在 $(pchs) 中列出的每个 .txt 文件都是最新的,并且 Make 决定不采取任何操作。
将它们添加到 .PHONY 目标中,以便在每次运行 Make:

.PHONY : $(pchs)

UPD 时强制重建它们。

还要检查 $(pchs) 列表不为空,可以按如下方式完成:

txt : $(pchs)
    @echo pchs is '$^'

That's because every .txt file you've listed in $(pchs) is up-to-date and Make decides to take no action.
Add them to .PHONY target to force rebuilding them every time you run Make:

.PHONY : $(pchs)

UPD.

Also check that $(pchs) list is not empty, it could be done i.e. as follows:

txt : $(pchs)
    @echo pchs is '$^'
安人多梦 2025-01-09 16:39:07

我将使用 Bash 来确定 *.txt 文件,而不是 Make:

txt:
        ls | grep -F '.txt'

您还可以使用它作为模板来创建更通用的目标,该目标会回显目录中存在的具有特定扩展名的任何文件。

您可能希望目标是 PHONY,因为它不创建文件。

I would use Bash to determine the *.txt files, instead of Make:

txt:
        ls | grep -F '.txt'

You could also use this as a template to make a more general target, that echos any files that exist in the directory with a particular extension.

You may want the target to be PHONY, since it's not making a file.

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