[Top][All Lists]

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

Re: 6.2 The Two Flavors of Variables

From: Paul Smith
Subject: Re: 6.2 The Two Flavors of Variables
Date: Wed, 23 Feb 2011 16:35:01 -0500

On Wed, 2011-02-23 at 11:54 +0330, ali hagigat wrote:
> Why it causes an infinite loop? The first time $(CFLAGS) is null so
> the right side will be expanded to "null -O". There will be no second
> time as null is a final value and CFLAGS becomes -O

Your statement "the first time $(CFLAGS) is null" indicates that you've
missing the critical aspect of recursive variables.

The right-hand side is NOT EXPANDED when the variable is assigned, at
all.  It's not expanded until the variable is referenced later on.  When
make reads that line above it doesn't expand $(CFLAGS), so its value at
that time (presumably null, yes) is irrelevant.

After that line, the CFLAGS variable has the value '$(CFLAGS) -O'.

The error happens later on, when someone else uses $(CFLAGS), for

        foo.o: foo.c
                $(CC) -o $@ -c $(CFLAGS) $<

Now make wants to expand $(CFLAGS) and it sees that the value of that
variable is '$(CFLAGS) -O'.  So it then tries to expand that, and sees
that $(CFLAGS) expands to '$(CFLAGS) -O'.  And so on until you run out
of memory.

However, GNU make actually has a maximum number of times it will allow
recursive expansion before it throws an error.  It used to fail on the
first recursion but it turns out that recursion can actually be useful
in conjunction with $(call ...) etc.

 Paul D. Smith <address@hidden>          Find some GNU make tips at:            
 "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]