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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值