makefile中tab与空格,及格式问题导致的错误

You have to understand that a makefile is really written in two completely different “languages”, in one file.
Recipes (the commands that run compilers, echo, etc.) are written in shell script syntax.
The rest of the makefile that is not in a recipe is written in makefile syntax.
In order for make to tell the difference between a recipe and things that are not a recipe, it uses TAB characters. So, lines that begin with TAB are assumed to be part of a recipe (so they are shell scripts and passed to the shell for parsing), and lines that do not begin with TAB cannot be part of a recipe (so they cannot be shell scripts: they must be make syntax).
In your examples, if [ -d … is shell syntax. If it appears in a makefile it must be part of a recipe, and so must be preceded by a TAB; if make tries to interpret this as makefile syntax it will be an error. ifneq is makefile syntax: if the shell tries to interpret this as a shell script it will be a syntax error, so it cannot be part of a recipe and must NOT be preceded by a TAB.
All other uses of indentation are optional and irrelevant (for example in your first example above you say “the next line should be indented by spaces”; that’s just a convention and the script will work exactly the same way whether or not you indent it at all).
Now, there are some details which get tricky: backslash-escaped newlines, rule contexts, etc. but if you stick with the rule that all recipe lines are indented with a TAB and no non-recipe lines are indented with a TAB, you’ll be OK.
出自:https://www.it1352.com/1592089.html

译:
makefile中有两种不同的语言,shell语法(recipe)和makefile语法(non-recipe),为了区分这两种语言所以使用tab。以tab开头的是shell(recipe)。
非tab的其他缩进用法都是可选的,无论是否缩进,脚本都将以完全相同的方式运行。
现在,有些细节变得棘手:反斜杠转义的换行符,规则上下文等。要记住一个规则,所有recipe都使用tab缩进,non-recipe不用tab缩进。

makefile编译时候出现:commands commence before first target
在C/C++语言中, 可以用\来换行, 此时要注意, 在一行的最后面加上, 而这个\后面不能再有任何字符。
对于makefile, 在\后多了空格, 结果编译出现:commands commence before first target

你可能感兴趣的:(makefile中tab与空格,及格式问题导致的错误)