3

My task is to pass multiple arguments to one of my executable binary files. For example, I have a binary which takes 6 arguments, so it works fine when I type:

./a.out 1 2 3 4 5 6

I want to do the same using a makefile so that when I type make INPUT=1 2 3 4 5 6 it should execute the a.out with all the six arguments in the INPUT. I can do this if I pass the arguments with escape characters like:

make INPUT=1\ 2\ 3\ 4\ 5\ 6

but is there any way to make it execute like

make INPUT=1 2 3 4 5 6

makefile contents:

@gcc prime.c
@./a.out ${INPUT}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Abhishek Gangwar
  • 1,697
  • 3
  • 17
  • 29
  • the list of parameters needs to be grouped. similar to -Dinput="1 2 3 4 5 6" However, a makefile is to create things, like the executable, not to execute the executable. – user3629249 Aug 27 '15 at 04:02
  • The argument grouping is done by the shell. Make only gets what what the shell passes it. The backslash is one way (usually the least desirable way) of escaping characters. If you have to type more than one backslash, use quotes instead, as in the accepted answer. Note that there's a second level of shell involved in the line that executes the program. If you needed to pass `"a multi-word phrase"` as a single argument to the program (as well as some other arguments), you'd have to work harder. – Jonathan Leffler Aug 27 '15 at 14:56

2 Answers2

3

Just put the args within quotations.

make INPUT="1 2 3 4 5 6"
kaylum
  • 13,833
  • 2
  • 22
  • 31
1

Additionally to how to handle arguments when passing via make:

If you need to pass a "multi-word phrase" as a single argument (as @jonathan-leffler suggested in a comment to the question), just put ${INPUT} part of your makefile into quotes:

@gcc prime.c
@./a.out "${INPUT}"

P.S. Both of single & double quotes are approachable.

Airat K
  • 55
  • 1
  • 1
  • 10