在if语句中设置变量不起作用

时间:2016-05-19 15:49:10

标签: makefile gnu-make

在这个非常简单的makefile中,我尝试在条件中设置一个变量,但是在变化之外没有注意到。

all:
        STUFF="nothing"
ifeq (true, true)
        echo "setting"
        STUFF="hi"
endif
        echo $(STUFF)

当我运行它时,我希望最后一个命令打印" hi",但它没有。

chris@cranberry ~/git % make
STUFF="nothing"
echo "setting"
setting
STUFF="hi"
echo 

我在这里做错了什么?

更新:

run: build/aura.bin
    ifdef DEBUGGING
        $(eval DFLAGS=-s -S)
    endif
    qemu-system-i386 -serial stdio $(DFLAGS) -kernel build/aura.bin

2 个答案:

答案 0 :(得分:1)

您可以在不eval

的情况下执行此操作
ifdef DEBUGGING
  run: private DFLAGS += -s -S
endif
run: build/aura.bin
    qemu-system-i386 -serial stdio $(DFLAGS) -kernel $<

另一种选择是

run: private DFLAGS += $(if $(DEBUGGING),-s -S)
run: build/aura.bin
    qemu-system-i386 -serial stdio $(DFLAGS) -kernel $<

删除private,使DFLAGS的先决条件中的run值可用。

答案 1 :(得分:0)

我使用$(eval <statement>)来设置值来修复它。

all:
        STUFF="nothing"
ifeq (true, true)
        echo "setting"
        $(eval STUFF="hi")
endif
        echo $(STUFF)