define variable in rule

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

define variable in rule

franksong
Hi

Can the variable be defined in rule ? Like this:

1 target :  $(OBJ_FILE)
2 TMP_LIB := $(shell cat $(OBJ_FILES))
3 $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@   $(TMP_LIB)
$(EXTERNAL_LIBS) $(SYSTEM_LIBS)

The OBJ_FILE is a text file with .o file list, and I want to translate
it to .o list used as the argument of link, so the variable TMP_LIB is
defined.

But when do Make, syntax error found in line 3. So I doubt the variable
can not be defined within a fule.

Who can help me on this? Thanks.

Frank



_______________________________________________
Help-make mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: define variable in rule

Paul Smith-20
%% <[hidden email]> writes:

  n> Can the variable be defined in rule ? Like this:

  n> 1 target :  $(OBJ_FILE)
  n> 2 TMP_LIB := $(shell cat $(OBJ_FILES))
  n> 3 $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@ $(TMP_LIB) $(EXTERNAL_LIBS) $(SYSTEM_LIBS)

  n> The OBJ_FILE is a text file with .o file list, and I want to translate
  n> it to .o list used as the argument of link, so the variable TMP_LIB is
  n> defined.

Why don't you just use:

  target :  $(OBJ_FILE)
  $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@ `cat $(OBJ_FILES)` $(EXTERNAL_LIBS) $(SYSTEM_LIBS)

?

  n> But when do Make, syntax error found in line 3. So I doubt the
  n> variable can not be defined within a fule.

You cannot, generally, define variables within rules, no.  The entire
content of the line after the TAB character is variable-expanded, then
passed to the shell.  So, these lines must be shell commands not make
commands.


If you have a sufficiently recent version of GNU make and are willing to
require that, you can use the $(eval ...) function inside a rule to
define a variable.

--
-------------------------------------------------------------------------------
 Paul D. Smith <[hidden email]>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.paulandlesley.org
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist


_______________________________________________
Help-make mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: define variable in rule

Jason Pearce
In reply to this post by franksong
I think you will find your answer here:
http://www.gnu.org/software/make/manual/html_chapter/make_6.html#SEC67

[hidden email] wrote:

>Hi
>
>Can the variable be defined in rule ? Like this:
>
>1 target :  $(OBJ_FILE)
>2 TMP_LIB := $(shell cat $(OBJ_FILES))
>3 $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@   $(TMP_LIB)
>$(EXTERNAL_LIBS) $(SYSTEM_LIBS)
>
>The OBJ_FILE is a text file with .o file list, and I want to translate
>it to .o list used as the argument of link, so the variable TMP_LIB is
>defined.
>
>But when do Make, syntax error found in line 3. So I doubt the variable
>can not be defined within a fule.
>
>Who can help me on this? Thanks.
>
>Frank
>
>
>
>_______________________________________________
>Help-make mailing list
>[hidden email]
>http://lists.gnu.org/mailman/listinfo/help-make
>
>  
>



_______________________________________________
Help-make mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: define variable in rule

John Graham-Cumming
In reply to this post by franksong
On Wed, 2005-06-08 at 23:54 +0800, [hidden email] wrote:

> Can the variable be defined in rule ? Like this:
>
> 1 target :  $(OBJ_FILE)
> 2 TMP_LIB := $(shell cat $(OBJ_FILES))
> 3 $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@   $(TMP_LIB)
> $(EXTERNAL_LIBS) $(SYSTEM_LIBS)
>
> The OBJ_FILE is a text file with .o file list, and I want to translate
> it to .o list used as the argument of link, so the variable TMP_LIB is
> defined.
>
> But when do Make, syntax error found in line 3. So I doubt the variable
> can not be defined within a fule.

Not like that it can't, but you could do this:

target: TMP_LIB := $(shell cat $(OBJ_FILES))
target:
        $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@   $(TMP_LIB) \
        $(EXTERNAL_LIBS) $(SYSTEM_LIBS)

(This has the side effect of defining TMP_LIB for any of target's
prerequisites.)

OTOH I don't see any real need for that TMP_LIB variable here (unless
it's being referenced in one of the other variables on the $(LD)
command-line) and so you could simplify your life greatly and just do:

target:
        $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@ $(shell cat $(OBJ_FILES)) \
        $(EXTERNAL_LIBS) $(SYSTEM_LIBS)

If you do that then you don't even need to bother with the $(shell)
because you could just use backticks and do

target:
        $(LD) $(OPT_LD_STD) $(LDFLAGS) -o $@ `cat $(OBJ_FILES)` \
        $(EXTERNAL_LIBS) $(SYSTEM_LIBS)

John.
--
John Graham-Cumming

Home: http://www.jgc.org/
Work: http://www.electric-cloud.com/
POPFile: http://getpopfile.org/
GNU Make Standard Library: http://gmsl.sf.net/




_______________________________________________
Help-make mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-make