1

我的makefile是:

CFLAGS=-g

all:        mcast_client mcast_server

mcast_client:   mcast_client.o $(ARG1)

mcast_server:   mcast_server.o

clean:
        rm -f mcast_client mcast_server mcast_client.o mcast_server.o

在我输入的命令窗口中,

$ make ARG1=hello, world!

这个对吗?

4

2 回答 2

1

利用:

$ make ARG1="hello, world!"
于 2013-08-08T22:59:38.500 回答
0

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.

于 2013-08-09T04:26:16.847 回答