对带有空格的文件名使用makefile通配符命令


8

我有一个用于压缩图片的makefile:

src=$(wildcard Photos/*.jpg) $(wildcard Photos/*.JPG)
out=$(subst Photos,Compressed,$(src))

all : $(out)

clean:
    @rmdir -r Compressed

Compressed:
    @mkdir Compressed

Compressed/%.jpg: Photos/%.jpg Compressed
    @echo "Compressing $<"
    @convert "$<" -scale 20% "$@"

Compressed/%.JPG: Photos/%.JPG Compressed
    @echo "Compressing $<"
    @convert "$<" -scale 20% "$@"

但是,例如,当我的图片名称中带有空格时Piper PA-28-236 Dakota.JPG,会出现此错误:

make: *** No rule to make target `Compressed/Piper', needed by `all'.  Stop.

我认为这是wildcard命令中的问题,但是我不确定要进行什么更改才能使其正常工作。

如何修改我的makefile以在文件名中保留空格?


我在这里在Stack Overflow上问过这个问题。
iBelieve 2012年

Answers:


4

我问了Stack Overflow,一个名叫perreal的用户帮助我解决了这个问题,是他的答案。

这是我为使其工作而要做的事情:

  1. 使用src=$(shell ls Photos | sed 's/ /?/g;s/.*/Photos\/\0/')固定的空间问题的wildcard命令,让目标,以工作为空格。

  2. 这会在结果文件中留下一个问号,因此请使用一个调用函数?将最终文件中的空格替换为:replace = echo $(1) | sed 's/?/ /g'。使用@convert "$<" -scale 20% "``$(call replace,$@)``"(我只使用了一个反引号,但是我不知道如何使其正确显示)来调用它。

所以,这是我的最终Makefile:

src=$(shell ls Photos | sed 's/ /?/g;s/.*/Photos\/\0/')
out=$(subst Photos,Compressed,$(src))

replace = echo $(1) | sed 's/?/ /g'

all : $(out)

clean:
    @rmdir -r Compressed

Compressed:
    @mkdir Compressed

Compressed/%.jpg: Photos/%.jpg Compressed
    @echo "Compressing $<"
    @convert "$<" -scale 20% "`$(call replace,$@)`"

Compressed/%.JPG: Photos/%.JPG Compressed
    @echo "Compressing $<"
    @convert "$<" -scale 20% "`$(call replace,$@)`"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.