views:

18

answers:

1

I have a makefile with the following code. I'm trying to set a variable in an if statement. It doesn't error, but %OPTION% just prints "%OPTION" (with only one percentage sign) and the $(OPTION) doesn't print anything. Please help

RELEASE_OR_DEBUG=debug
init:
SET OPTION=test
@echo test1 = %OPTION%
@echo test2 = $(OPTION)
if ".$(RELEASE_OR_DEBUG)" == ".debug" SET OPTION="-d"
@echo OPTION = %OPTION%
@echo OPTION = $(OPTION)

Output
test1 = %OPTION
test2 =
if ".debug" == ".debug" SET OPTION="-d"
OPTION = %OPTION
OPTION = $(OPTION)

A: 

You cannot set variables inside dependency statements (such as "init" in this case). In fact, you can't use if statements there either. Variables can be set by external batch files prior to calling the makefile (and that batch file then can call the makefile) or outside of dependency statements like RELEASE_OR_DEBUG in this example.
To access variables inside dependency statements, use $(VARIABLE_NAME). Be aware that if the variable name is not found, it will simply use a blank string (such as in your "@echo test2 = $(OPTION)" test where the variable OPTION was not found).

Dasmowenator