help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What's wrong about using variables in `define'?


From: Paul Smith
Subject: Re: What's wrong about using variables in `define'?
Date: Wed, 06 Aug 2008 08:57:20 -0400

On Wed, 2008-08-06 at 12:28 +0800, Pan ruochen wrote:
> But something seems wrong.
> 
> $cat Makefile
> define check
> tmpvar=$1
> $(warning $(tmpvar))
> tmpvar=$2
> $(warning $(tmpvar))
> endef
> $(eval $(call check,V1,V2))
> all:
> $make -f Makefile
> Makefile:7:
> Makefile:7:
> make: Nothing to be done for `all'.
> 
> But if I modify the script to:
> $cat Makefile
> define check
> $(warning $1)
> $(warning $2)
> endef
> $(eval $(call check,V1,V2))
> all:
> $make
> Makefile:4: V1
> Makefile:4: V2
> make: Nothing to be done for `all'.
> 
> Everything is okay. What's wrong about using variables in `define's?

Nothing.  In order to use call and eval you have to really think about
expansion and how it works.

What happens here is that call expands its argument first.  So, the
warning functions are being expanded during the call part of the
operation, BEFORE eval has run.  That's why those values are empty; they
haven't been set yet.  Then eval expands the results of the call
operation, and the variables are actually set.

In other words, it's doing exactly what you want in that those variables
ARE being set; it's just the warning function is not printing them.

You can "fix" this in one of two ways: either escape the warning
function so call doesn't expand it, so that eval will expand it:

        define check
        tmpvar=$1
        $$(warning $$(tmpvar))
        tmpvar=$2
        $$(warning $$(tmpvar))
        endef
        $(eval $(call check,V1,V2))
        all:

Or, just move the warning outside of the define altogether and run it
after the eval:

        define check
        tmpvar=$1
        tmpvar=$2
        endef
        $(eval $(call check,V1,V2))
        $(warning $(tmpvar))
        all:

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]