Makefile:带空格的先决条件 [英] Makefile: prerequisites with spaces

查看:122
本文介绍了Makefile:带空格的先决条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:GNU Make 3.81,Ubuntu 12.04

Update: GNU Make 3.81, Ubuntu 12.04

我有一组markdown文件,我希望将其编译为(例如)html文件,所以这是我的规则:

I have a set of markdown files that I want to compile to (say) html files, so this is my rule:

%.html: %.md
    pandoc $< -o $@

因此make foo.html会将foo.md转换为foo.html.

但是,源markdown文件名中有空格,并且我无法控制这些文件名,也就是说,我无法更改设置以删除空格.

However, there are spaces in the source markdown filenames and I do not have the ability to control these, that is I can't change a setting to remove the spaces.

这意味着如果我make foo\ bar.html,我会得到

This means if I make foo\ bar.html, I get

make: *** No rule to make target `foo bar.html'. Stop.

我如何编写通用规则%.html: %.md,其中前提条件文件名带有空格?

How can I write a generic rule %.html: %.md where the prerequisite filename has spaces?

我可以通过以下方法解决它:

I can get around it by using:

foo\ bar.html: foo\ bar.md
    pandoc $< -o $@

但是,当我想使用%构造时,我必须为我拥有的每个此类源文件手动写出此规则.我唯一希望做某种$(foreach f,$(get list of *.md files),$(eval $(call function_to_generate_rule)))吗?

But then I must manually write out this rule for every such source file that I have, when I'd rather use the % construct. Is my only hope to do some sort of $(foreach f,$(get list of *.md files),$(eval $(call function_to_generate_rule)))?

推荐答案

从@binki说来的看来,GNU make 3.82可能没有此问题,但是不幸的是,我没有选择从v3.81更新在我的Ubuntu 12.04计算机上.

It seems from what @binki says that GNU make 3.82 might not have this issue, but unfortunately I do not have the option to update from v3.81 that is on my Ubuntu 12.04 machine.

我设法通过使用SECONDEXPANSION在先决条件中将空格替换为反斜杠空间来解决它(因此,foo bar.md的先决条件变为foo\ bar.md).

I managed to "solve" it like so by using SECONDEXPANSION to substitute spaces with backslash-space in the prerequisite (so a prerequisite of foo bar.md becomes foo\ bar.md).

# define a variable with a single space
space:=
space+=

.SECONDEXPANSION:
%.html: $$(subst $$(space),\$$(space),%).md
   pandoc "$<" -o "$@"

此处是日志.同样,可以在Ubuntu 12.04/GNU Make 3.81上运行,如果您拥有3.82,则可以使用@binki的解决方案,它看起来更优雅.

Here is the log. Again, works on Ubuntu 12.04/GNU Make 3.81, perhaps if you have 3.82 you can use @binki's solution which seems more elegant.

这篇关于Makefile:带空格的先决条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆