11

If I have a list of n commands, c = c1 ... cn, how can I execute them in order for a given target? I tried the foreach construct

$(foreach x,$(c),./$(x))

but that puts all the commands on one line. Any clues?

1
  • 1
    Sounds like you want bash for that more than Make. Commented Sep 21, 2012 at 10:20

2 Answers 2

16

You identified the problem (“but that puts all the commands on one line”). You just need to append a newline whenever you expand $x in your loop.

define \n


endef

Now simply use $(foreach x,$c,./$(x)${\n}) in your recipe.

Sign up to request clarification or add additional context in comments.

Comments

15

If there is no need to check for success, then adding semicolon should work:

$(foreach x,$c,./$(x);)

If you need to fail if one of the command in the list returns failure, you need to break it in steps. Instead of directly executing the commands, we wrap the execution in a make function:

define execute-command
$(1)

endef

execute-list:
        $(foreach x,$(c),$(call execute-command,./$(x)))

2 Comments

You don't need to prefix the expansion of each line with a tab. Make realises each line represents a new command in the recipe due to the tab already.
@bobbogo - nice, thanks. Simplified the answer per your suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.