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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章