3.1.1 Splitting Long Lines(拆分长行)
Makefiles use a “line-based” syntax in which the newline(换行符) character is special and marks the end of a statement(makefile使用换行符作为语句的结束). GNU make
has no limit on the length of a statement line, up to the amount of memory in your computer(make对语句行的长度没有限制,由你电脑内存的大小所决定).
However, it is difficult to read lines which are too long to display without wrapping or scrolling. So, you can format your makefiles for readability by adding newlines into the middle of a statement: you do this by escaping(转义) the internal newlines with a backslash (\
) character. Where we need to make a distinction(区别) we will refer to “physical lines” as a single line ending with a newline (regardless of whether it is escaped) and a “logical line” being a complete statement including all escaped newlines up to the first non-escaped newline(在语句的中间加一个反斜杠+回车符,可以使语句分行显示).
The way in which backslash/newline combinations are handled depends on whether the statement is a recipe line or a non-recipe line. Handling of backslash/newline in a recipe line is discussed later(反斜杠+回车符的处理方式取决于语句是命令行语句还是非命令行语句).
Outside of recipe lines, backslash/newlines are converted into a single space character(在命令行语句之外,反斜杠+回车符这个组合被转换为一个空格字符). Once that is done, all whitespace around the backslash/newline is condensed(压缩) into a single space: this includes all whitespace preceding the backslash, all whitespace at the beginning of the line after the backslash/newline, and any consecutive(连续不断的) backslash/newline combinations.
If the .POSIX
special target is defined then backslash/newline handling is modified slightly to conform to POSIX.2: first, whitespace preceding a backslash is not removed and second, consecutive backslash/newlines are not condensed.
Splitting Without Adding Whitespace(拆分语句不带空格)
If you need to split a line but do not want any whitespace added, you can utilize(使用) a subtle(巧妙的) trick(技巧): replace your backslash/newline pairs with the three characters dollar sign/backslash/newline:
var := one$\
word
After make
removes the backslash/newline and condenses(压缩) the following line into a single space, this is equivalent to:
var := one$ word
Then make
will perform variable expansion(变量展开). The variable reference ‘$ ’ refers to a variable with the one-character name “ ” (space) which does not exist, and so expands to the empty string, giving a final assignment which is the equivalent of:
var := oneword