Your invocation make ARG1="hello, world!"
is almost correct (but the quotes are needed for the shell), but your Makefile
is not.
A more realistic approach would be to pass the message as a preprocessor macro.
Assume hello.c
contains (in the middle of some C function)
printf("%s\n", MESSAGE);
Then you could have a Makefile
rule like
hello.o: hello.c
$(COMPILE.c) -DMESSAGE=\"$(MSG)\" $< -o $@
The quotes are backslashed because the preprocessor macro MESSAGE
should have quotes.
And finally you could invoke
make "MSG=hello friend"
Beware that this won't work if MSG
contains quotes "
or backslashes \
.... In the command above the quotes are interpreted by the shell...
Notice that you are supposed to invoke make
with the same command every time... (since hello.o
won't be rebuilt if the MSG
has changed).
BTW, take the habit of always compiling with -Wall
so
CFLAGS= -Wall -g
and look at predefined rules of make
given by make -p
Consider using remake (notably invoked with -x
) to debug tricky or complex Makefile
-s.