Makefile 中的 foreach 函数

Foreach function in Makefile

我试图使用以下代码在 makefile 中演示 foreach 函数的结果:

dirs := ../../Downloads ../../Documents ../special-var ../subdir-test

files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))

all : ; @echo all-files: $(files)

执行时出现以下错误:

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `echo all-files:
../../Downloads/install svnentos6.mp4 
../../Downloads/Abacus-Fees.xls 
../../Downloads/DiskOnRAM 
../../Downloads/Device Driver(FPGA) 
../../Downloads/GNU make and Makefiles.mp4 
../../Downloads/openwrt 
../../Downloads/Study Materials 
../../Downloads/Images 
../../Downloads/spread-src-4.4.0.tar.gz 

Makefile-foreach-func:7: recipe for target 'all' failed
make: *** [all] Error 1

如果我从目录中删除前两个参数。然后它没有错误。或者,如果我只将参数添加到一个目录 (../),那么也没有错误。但是对于向上的两个目录 (../../) 它会抛出错误。 有人可以帮我解决吗。谢谢

错误来自(shell 运行)echo,而不是来自make。与以往一样,您需要引用包含特殊字符(如括号)的字符串(通常是 pretty much everything)。