Variable
Makefile - 在一個階段設置多個變數
我有這個Makefile,我在簡單地設置一些變數時遇到了一些麻煩:
my_stage: echo "FULL_NAME=$(FULL_NAME)" echo "MY_NAME=$(MY_NAME)" $(eval SOME_NAME=$(shell sh -c "echo ${FULL_NAME} | cut -d"-" -f 2")) echo "SOME_NAME=$(SOME_NAME)" $(eval NAME_ONLY=$(shell sh -c "echo ${SOME_NAME}-only)) echo "NAME_ONLY=$(NAME_ONLY)" $(eval RIGHT_NAME=$(shell sh -c "echo ${SOME_NAME}-right)) $(eval NAME_APPENDED=$(shell sh -c "echo ${RIGHT_NAME}.${MY_NAME})) echo "NAME_APPENDED=$(NAME_APPENDED)"
假裝的結果是:
FULL_NAME=Shop-with-me MY_NAME=Mariana SOME_NAME=with NAME_ONLY=with-only NAME_APPENDED=with-right.Mariana
但是,目前的結果是:
FULL_NAME=Shop-with-me MY_NAME=Mariana SOME_NAME=with NAME_ONLY= NAME_APPENDED=
有人可以幫我弄清楚發生了什麼嗎?我做錯了什麼?
我也已經嘗試過了(但沒有成功):
my_stage: echo "FULL_NAME=$(FULL_NAME)" echo "MY_NAME=$(MY_NAME)" $(eval SOME_NAME=$(shell sh -c "echo ${FULL_NAME} | cut -d"-" -f 2")) echo "SOME_NAME=$(SOME_NAME)" NAME_ONLY = ${SOME_NAME}-only echo "NAME_ONLY=$(NAME_ONLY)" RIGHT_NAME = ${SOME_NAME}-right NAME_APPENDED = ${RIGHT_NAME}.${MY_NAME} echo "NAME_APPENDED=$(NAME_APPENDED)"
當您在命令中使用大括號(如
${FOO}
)時,您指的是一個shell變數,如 shell 呼叫中所定義的make
。當您
$(fOO)
在命令中使用括號(如 )時,您指的是 make 的變數。由於您只設置了 make 的變數,顯然對同名 shell 變數的引用會導致空值。
所以
$(eval NAME_ONLY=$(shell sh -c "echo $(SOME_NAME)-only))
應該工作。簡單
$(eval NAME_ONLY = $(SOME_NAME)-only)
應該在規則中起作用。您不需要為此呼叫 shell。您還可以考慮使用 make 的文本函式,例如
subst
.