无法从 Makefile 调用函数
Unable to call function from a Makefile
我需要从 make 目标调用一个函数,这个函数会被调用多次,
define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then
echo "" > $(2).txt
else
echo "It is Not Setup";
fi
endef
all:
$(call generate_file,John Doe,101)
$(call generate_file,Peter Pan,102)
现在我陷入了这个错误:
bash-5.0# make
if [ "" = "ci" ]; then
/bin/sh: syntax error: unexpected end of file (expecting "fi")
make: *** [Makefile:10: all] Error 2
您的函数是多行的,它将尝试作为单独的 shell 调用执行。这将失败,因为任何单行本身在语法上都不正确。您可以通过在一行中设置它来使其工作,即:
$ cat Makefile
define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then \
echo "" > $(2).txt; \
else \
echo "It is Not Setup"; \
fi
endef
all:
$(call generate_file,John Doe,101)
$(call generate_file,Peter Pan,102)
输出:
$ make
if [ "" = "ci" ]; then echo "John Doe" > 101.txt; else echo "It is Not Setup"; fi
It is Not Setup
if [ "" = "ci" ]; then echo "Peter Pan" > 102.txt; else echo "It is Not Setup"; fi
It is Not Setup
我需要从 make 目标调用一个函数,这个函数会被调用多次,
define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then
echo "" > $(2).txt
else
echo "It is Not Setup";
fi
endef
all:
$(call generate_file,John Doe,101)
$(call generate_file,Peter Pan,102)
现在我陷入了这个错误:
bash-5.0# make
if [ "" = "ci" ]; then
/bin/sh: syntax error: unexpected end of file (expecting "fi")
make: *** [Makefile:10: all] Error 2
您的函数是多行的,它将尝试作为单独的 shell 调用执行。这将失败,因为任何单行本身在语法上都不正确。您可以通过在一行中设置它来使其工作,即:
$ cat Makefile
define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then \
echo "" > $(2).txt; \
else \
echo "It is Not Setup"; \
fi
endef
all:
$(call generate_file,John Doe,101)
$(call generate_file,Peter Pan,102)
输出:
$ make
if [ "" = "ci" ]; then echo "John Doe" > 101.txt; else echo "It is Not Setup"; fi
It is Not Setup
if [ "" = "ci" ]; then echo "Peter Pan" > 102.txt; else echo "It is Not Setup"; fi
It is Not Setup