"premature end of file" 在 GNU makefile 中是什么意思?

What does "premature end of file" mean in GNU makefile?

我正在尝试练习 makefile 中的 if 语句,但它一直说文件过早结束。 代码:

test_rule:
   if  [ yes ]
       echo "if works"
   endif

输出:

make test_rule
/bin/sh: -c: line 1: syntax error : premature end of file
make: *** [Makefile:13: test_rule] Error 1

网上试了很多代码(比如把[]换成()或者在echo前面加@)都没有用。

作为the manual explains,您需要使用制表符(默认):

Please note: you need to put a tab character at the beginning of every recipe line! This is an obscurity that catches the unwary. If you prefer to prefix your recipes with a character other than tab, you can set the .RECIPEPREFIX variable to an alternate character (see Special Variables).

因此,你需要这样写:

foo:
    gcc

换句话说就是字符串foo:\n\tgcc\n.

请注意,Whosebug 在呈现 Markdown 时将制表符转换为 4 个空格(但如果您尝试编辑此答案,您将看到制表符)。

其他 answers/comments 建议您的 'tabbing' 不正确 -- 这可能是真的 -- 但查看错误消息...

make test_rule
/bin/sh: -c: line 1: syntax error : premature end of file
make: *** [Makefile:13: test_rule] Error 1

我认为这只是命令中 shell 语法不正确的情况。请记住,一般来说,make 配方的命令部分中的每一行在它自己的 shell 中都是 运行。尝试以下方法(记住我不是 shell 专家)...

test_rule:
    if [ yes ]; then \
      echo "it works"; \
    fi