这是我的makefile:

all:ll

ll:ll.c   
  gcc  -c  -Wall -Werror -02 c.c ll.c  -o  ll  $@  $<

clean :
  \rm -fr ll

当我尝试make clean或make make时,我得到这个错误:

:makefile:4: *** missing separator.  Stop.

我该怎么解决呢?


当前回答

默认情况下,您应该始终在制表符后写入命令,而不是空格。可以将其更改为带有. recipeprefix变量的另一个字符。

这适用于gcc行(行#4)。您需要在gcc之前插入tab。

也用rm -fr ll替换\rm -fr ll。在此命令之前也要插入制表符。

其他回答

当你在VSCode中创建Makefile时,你应该设置Tab Size: 4。

这是因为制表符被空格取代。 若要禁用此功能,请转到

中- >编辑- >首选项- >编辑器

并删除检查

"用空格替换制表符"

如果你的编辑器/IDE支持它(很可能是支持的!),帮你自己一个忙,让它成为你的.editorconfig的永久成员。

[Makefile]
indent_style = tab

Make定义了开始每个食谱所需的TAB。每条规则的所有操作都由制表符标识。如果您喜欢使用制表符以外的字符作为食谱的前缀,则可以将. recipeprefix变量设置为另一个字符。

为了检查,我使用命令cat -e -t -v makefile_name。

它显示了以^I结尾的制表符和以$结尾的行。这两者对于确保依赖关系正确结束至关重要,并且制表符标记规则的操作,以便make实用程序容易识别它们。

例子:

Kaizen ~/so_test $ cat -e -t -v  mk.t
all:ll$      ## here the $ is end of line ...                   
$
ll:ll.c   $
^Igcc  -c  -Wall -Werror -02 c.c ll.c  -o  ll  $@  $<$ 
## the ^I above means a tab was there before the action part, so this line is ok .
 $
clean :$
   \rm -fr ll$
## see here there is no ^I which means , tab is not present .... 
## in this case you need to open the file again and edit/ensure a tab 
## starts the action part

TLDR;

Makefile语法可能很古怪 如果你想让一行代码被解释为make代码,那么它只能用空格缩进。 如果您希望将一行代码解释为bash代码,则必须只使用制表符缩进

sometask:
  ifeq ($FOO,bar)  // this is make code. only spaces
    echo "foobar"  // this is bash code. only tabs
  endif            // again, this is make code. only spaces

从技术上讲,它是指示解释器的首缩进。