Makefile文件函数演示错误

Makefile file function demo errors

我试图用一个小的 makefile 为我的演示在 makefile 中获取文件函数的结果:

CMD = cat

OBJECTS = Makefile Makefile-filter-func

program : $(OBJECTS)
        $(file >$@.in) $(foreach O,$^,$(file >>$@.in,$O))
        @echo The file has been created.
all : 
        $(CMD) $(CMDFLAGS) @$@.in
        @echo The file contents are printed.
        @rm $@.in
        @echo The file removed.

我想使用 ls 命令查看文件的文件名,但是这个 makefile 有以下错误:

Makefile-file-func:7: *** recipe commences before first target.  Stop.

我哪里错了。

可以在 make(版本 3.82)的源代码中找到指向答案的指针,在文件 read.c:

989      /* This line starts with a tab but was not caught above because there
990         was no preceding target, and the line might have been usable as a
991         variable definition.  But now we know it is definitely lossage.  */
992      if (line[0] == cmd_prefix)
993        O (fatal, fstart, _("recipe commences before first target"));

有了这些信息,就可以通过在正确的位置插入 space 来重现您的问题。在下面的代码中,~ 表示 space 并且 <TAB> 表示 TAB:

program : $(OBJECTS)
~~~~~~~~$(file >$@.in) $(foreach O,$^,$(file >>$@.in,$O))
 <TAB>  @echo The file has been created.

由于 space 和制表符在您的问题中丢失了,因此很难判断这是否正是您的情况。

请注意,食谱通常应以 TAB 开头。