GNU make的foreach函數
foreach函數僅GNU make支持:
下面的代碼中使用了函數foreach和shell
- files=main.exe a.exe b.exe
- all:
- echo $(files); \
- rm -f $(foreach i, $(shell echo $(files) | sed s/.exe//g), $(i).o)
shell 循環
以下代碼實現與上面同樣的功能, 該版本的循環, 在多平台(AIX, HP-UX, SUSE)測試沒有問題:
- files=main.exe a.exe b.exe
- all:
- for name in `echo $(files) | sed s/.exe//g`; \
- do \
- rm -f "$$name".o; \
- done
注意, 在makefile中的shell變量要用2個$號表示變量名稱
