【问题标题】:Unable to call function from a Makefile无法从 Makefile 调用函数
【发布时间】:2020-09-10 10:20:47
【问题描述】:

我需要从make目标调用一个函数,这个函数会被多次调用,

define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then
    echo "$1" > $(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 makefile gnu-make


    【解决方案1】:

    您的函数是多行的,它将尝试作为单独的 shell 调用执行。这将失败,因为任何单行本身在语法上都不正确。您可以通过在一行中设置它来使其工作,即:

    $ cat Makefile
    define generate_file
    if [ "${RQM_SETUP}" = "ci" ]; then \
        echo "$1" > $(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
    

    【讨论】:

    • 嘿@raspy 谢谢你的清理,现在上面的代码工作了,所以尝试用我想做的实际东西来实现它,并且在通过 RQM_SETUP=ci 时,它可以工作正如预期的那样,但其他部分失败了,
    • @MohammedAli 请不要那样做。 raspy 回答了你原来的问题。如果您因此遇到问题,那应该是一个新问题。
    猜你喜欢
    • 2012-10-18
    • 2020-05-07
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2021-08-31
    • 2013-03-20
    • 2018-04-08
    • 2013-05-29
    相关资源
    最近更新 更多