help-make
[Top][All Lists]
Advanced

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

Re: Assigning $@ to target-specific variable


From: Paul D. Smith
Subject: Re: Assigning $@ to target-specific variable
Date: Thu, 23 Feb 2006 11:55:26 -0500

%% Ryan Berdeen <address@hidden> writes:

  rb> I've just started using GNU make (3.80), and I'm confused why the  
  rb> following doesn't work:

  rb> $(TARGETS): target := $@

  rb> The target variable remains empty.

That's because you're using simple assignment, which evaluates the right
hand side immediately as the makefile is read in.  See the section on
"How make Reads a Makefile" in the GNU make manual to understand the
difference between immediate and deferred expansion.

  rb> According to the manual, "All variables that appear within the  
  rb> variable-assignment are evaluated within the context of the  
  rb> target" (http://www.gnu.org/software/make/manual/html_chapter/ 
  rb> make_6.html#SEC77). As far as I have been able to determine, inside  
  rb> the context of the target, $@ contains the name of the target.

Yes... but that value is not set until make actually tries to build the
target.

By using := you're forcing to the expansion to happen when the makefile
is read; at that time the automatic variables are not set yet.  I
suppose this isn't as clear as it could be in the manual.

  rb> target1: target = target1
  rb> target2: target = target2

If you would have written your first example like this, using recursive
variable assignment (=) instead of simple assignment (:=), it would have
worked:

    target1 : target = $@

will do what you want.


However, I find this whole thing rather useless... why don't you just
use $@ directly instead of creating a different variable named "target"
for it?

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          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




reply via email to

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