Makefile variable initialization and export

后端 未结 4 1787
野趣味
野趣味 2021-02-02 08:00
somevar := apple
export somevar
update := $(shell echo \"v=$$somevar\")

all:
    @echo $(update)

I was hoping to apple as output of command, however i

4条回答
  •  太阳男子
    2021-02-02 08:34

    The problem is that export exports the variable to the subshells used by the commands; it is not available for expansion in other assignments. So don't try to get it from the environment outside a rule.

    somevar := apple
    export somevar
    
    update1 := $(shell perl -e 'print "method 1 $$ENV{somevar}\n"')
    # Make runs the shell command, the shell does not know somevar, so update1 is "method 1 ".
    
    update2 := perl -e 'print "method 2 $$ENV{somevar}\n"'
    # Now update2 is perl -e 'print "method 2 $$ENV{somevar}\n"'
    
    # Lest we forget:
    update3 := method 3 $(somevar)
    
    all:
        echo $(update1)
        $(update2)
        echo $(update3)
        perl -e 'print method 4 "$$ENV{somevar}\n"'
    

提交回复
热议问题