解析 Makefile 通配符 % 内部 shell 函数
在调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用GNU make的二次扩展功能:
You can use secondary expansion feature of GNU make:
使
foo.txt
以Makefile
格式包含foo.exe
的依赖项:然后在您的文件中使用
include
行Makefile
:最后,更新您的模式规则:
Make
foo.txt
contain the dependencies forfoo.exe
inMakefile
format:Then use an
include
line in yourMakefile
:Lastly, update your pattern rule:
我认为你正在尝试这样做:
I think you're trying to do this:
那么,从外部文件加载(动态生成的)依赖项的常用方法是使用 include。也就是说,您可以修复您的示例,您只需在 %.txt 文件生成期间添加目标即可。在 Makefile 中你可以这样写:
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:
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.