解析 Makefile 通配符 % 内部 shell 函数

发布于 2024-09-16 22:32:46 字数 265 浏览 5 评论 0原文

在调用 shell 函数之前,make 似乎不会解析通配符 %,在这种情况下:

%.exe: $(shell cat %.txt)
    gcc $? -o $@

如果我在 shell 上键入此内容:

$ make test.exe

shell 抱怨它找不到“%.txt”,但我期待它找到“test.txt”。

有没有办法解决这个问题?

谢谢!

it seems that make doesn't resolve the wildcard character % before calling the shell function, in cases like this one:

%.exe: $(shell cat %.txt)
    gcc $? -o $@

if I type this on the shell:

$ make test.exe

the shell complains that it cannot find "%.txt," but I was expecting it to find "test.txt".

is there anyway to workaround that?

thanks!

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

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

发布评论

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

评论(4

小巷里的女流氓 2024-09-23 22:32:46

您可以使用GNU make的二次扩展功能:

$ cat Makefile
all : x.exe
.PHONY: all
.SECONDEXPANSION:
%.exe: $(shell cat $(basename $(@F)).txt)
    @echo "$@ depends on $^"

$ cat x.txt 
a
b
c

$ touch a b c

$ make
x.exe depends on a b c

You can use secondary expansion feature of GNU make:

$ cat Makefile
all : x.exe
.PHONY: all
.SECONDEXPANSION:
%.exe: $(shell cat $(basename $(@F)).txt)
    @echo "$@ depends on $^"

$ cat x.txt 
a
b
c

$ touch a b c

$ make
x.exe depends on a b c
殤城〤 2024-09-23 22:32:46

使 foo.txtMakefile 格式包含 foo.exe 的依赖项:

foo.exe: foo.o bar.o baz.o

然后在您的文件中使用 includeMakefile

include foo.txt

最后,更新您的模式规则:

%.exe:
    gcc -o $@ $^

Make foo.txt contain the dependencies for foo.exe in Makefile format:

foo.exe: foo.o bar.o baz.o

Then use an include line in your Makefile:

include foo.txt

Lastly, update your pattern rule:

%.exe:
    gcc -o $@ $^
写给空气的情书 2024-09-23 22:32:46

我认为你正在尝试这样做:

.SUFFIXES: .exe .txt
.txt.exe:
        gcc $? -o $@

I think you're trying to do this:

.SUFFIXES: .exe .txt
.txt.exe:
        gcc $? -o $@
天暗了我发光 2024-09-23 22:32:46

那么,从外部文件加载(动态生成的)依赖项的常用方法是使用 include。也就是说,您可以修复您的示例,您只需在 %.txt 文件生成期间添加目标即可。在 Makefile 中你可以这样写:

DEPS = $(OBJS:.o=.txt)

-include $(DEPS)

BTW,这些依赖文件通常使用的后缀是 %.d。

以下网站展示了一种将依赖项生成集成到 Makefile 中的智能方法。 即依赖项文件然后根据需要生成并更新。不需要“make deps”步骤或类似的东西。

Well, the usual way to load (dynamically generated) dependencies from an external file is to use include. I.e. you can fix your example, you can just add the destination(s) during your %.txt file generation. In the Makefile you can write then something like this:

DEPS = $(OBJS:.o=.txt)

-include $(DEPS)

BTW, the usually used suffix for these dependency file is %.d.

Following web site shows an intelligent approach to integrate the dependency generation in your Makefile. I.e. the dependency files are then generated and updated as needed. No need for an 'make deps' step or something like that.

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