Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables defined *and* undefined

Tags:

makefile

I guess this makes sense somehow, but I can't grasp why: In the following code, I get both warnings (note that the original code was indented with tabs):

define variable-definition
    ifndef $1
        $(warning $1 is undefined)
    else
        $(warning $1 is defined)
    endif
endef

PS: I want to check whether the variable with the name passed as $1 exists, not whether $1 was passed.

PPS: Dedenting the entire thing doesn't help.

like image 907
l0b0 Avatar asked Feb 26 '11 14:02

l0b0


People also ask

Which is undefined variable?

An undefined variable in the source code of a computer program is a variable that is accessed in the code but has not been declared by that code. In some programming languages, an implicit declaration is provided the first time such a variable is encountered at compile time.

What is the difference between defined and undefined?

The difference between defined and undefined articles is therefore in the object or person that is designated. If the object or person is known and identified, the definite articles are used. On the contrary, if the object or person is not known and identified, the undefined articles are used.

What are defined and undefined variables in JavaScript?

Undefined: It occurs when a variable has been declared but has not been assigned with any value. Undefined is not a keyword. Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using var or const keyword.

Why is my variable undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .


1 Answers

Beta's analysis of the root cause is correct, you're not escaping your $ on the $(warning) calls. Here's how I'd fix it:

define variable-def
  ifndef $1
    $$(warning $1 is undefined)
  else
    $$(warning $1 is defined)
  endif
endef
FOO=bar
$(eval $(call variable-def,FOO))

Note that I am indenting with spaces, not tabs. If you indent with tabs, you get this error: *** commands commence before first target. Stop.

This uses GNUisms, but so does your sample (I think).

like image 176
Jack Kelly Avatar answered Oct 11 '22 14:10

Jack Kelly