makefile中的循环控制

GNU make的foreach函数

foreach函数仅GNU make支持:

下面的代码中使用了函数foreach和shell

 
  1. files=main.exe a.exe b.exe  
  2. all:  
  3.     echo $(files);   
  4.     rm -f $(foreach i, $(shell echo $(files) | sed s/.exe//g), $(i).o)  

shell 循环

以下代码实现与上面同样的功能, 该版本的循环, 在多平台(AIX, HP-UX, SUSE)测试没有问题:

 
  1. files=main.exe a.exe b.exe  
  2.   
  3. all:  
  4.     for name in `echo $(files) | sed s/.exe//g`;   
  5.     do   
  6.         rm -f "$$name".o;   
  7.     done  


注意, 在makefile中的shell变量要用2个$号表示变量名称

原文地址:https://www.cnblogs.com/catgatp/p/6527617.html