如何通过遍历列表在Makefile中生成目标? [英] How to generate targets in a Makefile by iterating over a list?

查看:111
本文介绍了如何通过遍历列表在Makefile中生成目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

LIST=0 1 2 3 4 5
PREFIX=rambo

# some looping logic to interate over LIST

预期结果:

rambo0:
    sh rambo_script0.sh

rambo1:
    sh rambo_script1.sh

由于我的LIST有6个元素,因此应生成6个目标.将来,如果我想添加更多目标,我希望能够只修改LIST而不接触代码的任何其他部分.

Since my LIST has 6 elements, 6 targets should be generated. In future, if I want to add more targets, I want to be able to just modify my LIST and not touch any other part of the code.

应该如何编写循环逻辑?

How should the looping logic be written?

推荐答案

使用

Use text-transforming functions. With patsubst you can make quite general transformations. For constructing filenames, addsuffix and addprefix are both convenient.

对于规则,请使用样式规则.

总体结果可能如下所示:

The overall result might look something like this:

LIST = 0 1 3 4 5
targets = $(addprefix rambo, $(LIST))

all: $(targets)

$(targets): rambo%: rambo%.sh
    sh $<

这篇关于如何通过遍历列表在Makefile中生成目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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