日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

GNU make manual 翻译( 一百五十五)

發布時間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GNU make manual 翻译( 一百五十五) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

繼續翻譯

6.2 The Two Flavors of Variables ================================There are two ways that a variable in GNU `make' can have a value; we call them the two "flavors" of variables. The two flavors are distinguished in how they are defined and in what they do when expanded.The first flavor of variable is a "recursively expanded" variable. Variables of this sort are defined by lines using `=' (*note Setting Variables: Setting.) or by the `define' directive (*note Defining Multi-Line Variables: Multi-Line.). The value you specify is installed verbatim; if it contains references to other variables, these references are expanded whenever this variable is substituted (in the course of expanding some other string). When this happens, it is called "recursive expansion".For example,foo = $(bar)bar = $(ugh)ugh = Huh?all:;echo $(foo)will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to `$(ugh)' which finally expands to `Huh?'.This flavor of variable is the only sort supported by other versions of `make'. It has its advantages and its disadvantages. An advantage (most would say) is that:CFLAGS = $(include_dirs) -Oinclude_dirs = -Ifoo -Ibarwill do what was intended: when `CFLAGS' is expanded in a recipe, it will expand to `-Ifoo -Ibar -O'. A major disadvantage is that you cannot append something on the end of a variable, as inCFLAGS = $(CFLAGS) -Obecause it will cause an infinite loop in the variable expansion. (Actually `make' detects the infinite loop and reports an error.) Another disadvantage is that any functions (*note Functions for Transforming Text: Functions.) referenced in the definition will be executed every time the variable is expanded. This makes `make' run slower; worse, it causes the `wildcard' and `shell' functions to give unpredictable results because you cannot easily control when they are called, or even how many times.To avoid all the problems and inconveniences of recursively expanded variables, there is another flavor: simply expanded variables."Simply expanded variables" are defined by lines using `:=' (*note Setting Variables: Setting.). The value of a simply expanded variable is scanned once and for all, expanding any references to other variables and functions, when the variable is defined. The actual value of the simply expanded variable is the result of expanding the text that you write. It does not contain any references to other variables; it contains their values _as of the time this variable was defined_. Therefore,x := fooy := $(x) barx := lateris equivalent toy := foo barx := laterWhen a simply expanded variable is referenced, its value is substituted verbatim.Here is a somewhat more complicated example, illustrating the use of `:=' in conjunction with the `shell' function. (*Note The `shell' Function: Shell Function.) This example also shows use of the variable `MAKELEVEL', which is changed when it is passed down from level to level. (*Note Communicating Variables to a Sub-`make': Variables/Recursion, for information about `MAKELEVEL'.) ifeq (0,${MAKELEVEL})whoami := $(shell whoami)host-type := $(shell arch)MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}endifAn advantage of this use of `:=' is that a typical `descend into a directory' recipe then looks like this: ${subdirs}:${MAKE} -C $@ allSimply expanded variables generally make complicated makefile programming more predictable because they work like variables in most programming languages. They allow you to redefine a variable using its own value (or its value processed in some way by one of the expansion functions) and to use the expansion functions much more efficiently (*note Functions for Transforming Text: Functions.).You can also use them to introduce controlled leading whitespace into variable values. Leading whitespace characters are discarded from your input before substitution of variable references and function calls; this means you can include leading spaces in a variable value by protecting them with variable references, like this:nullstring :=space := $(nullstring) # end of the lineHere the value of the variable `space' is precisely one space. The comment `# end of the line' is included here just for clarity. Since trailing space characters are _not_ stripped from variable values, just a space at the end of the line would have the same effect (but be rather hard to read). If you put whitespace at the end of a variable value, it is a good idea to put a comment like that at the end of the line to make your intent clear. Conversely, if you do _not_ want any whitespace characters at the end of your variable value, you must remember not to put a random comment on the end of the line after some whitespace, such as this:dir := /foo/bar # directory to put the frobs inHere the value of the variable `dir' is `/foo/bar ' (with four trailing spaces), which was probably not the intention. (Imagine something like `$(dir)/file' with this definition!) There is another assignment operator for variables, `?='. This is called a conditional variable assignment operator, because it only has an effect if the variable is not yet defined. This statement:FOO ?= baris exactly equivalent to this (*note The `origin' Function: Origin Function.):ifeq ($(origin FOO), undefined)FOO = barendifNote that a variable set to an empty value is still defined, so `?=' will not set that variable.

6.2 變量的兩個風格
================================

有兩種方法可以使得GNU make中的一個變量擁有一個值;我們成其為變量的兩個風格。這兩個趣味由他們如何定義和如何擴展而區分開來。

第一個變量的趣味來自遞歸擴展變量。此類變量用 =來定義(*note Setting Variables: Setting),或者用 define 指令(*note Defining Multi-Line Varaibles: Multi-Line)

你所定義的值會被逐字翻譯;如果它包含了對其他變量的參照。這些參照會在此變量被替換的時候進行擴展(在對其他字符串的擴展時)。當它發生的時候,可以稱為遞歸擴展。

例如,

foo = $(bar)
bar = $(ugh)
ugh = Huh?

all:;echo $(foo)

會顯示`Huh?': `$(foo)' 擴展為 `$(bar)',它再繼續擴展為`$(ugh)' ,最終擴展為`Huh?'。

This flavor of variable is the only sort supported by other versions
of `make'. It has its advantages and its disadvantages. An advantage
(most would say) is that:

這種風格的變量是唯一被其他版本make所支持的。它有優點和缺點。一個優點(大多數情況下)是:

CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar

將會執行所期待的: 當 CFLAGS 在片段中被擴展,它會擴展為 ?-Ifoo -Ibar -O。一個主要的確定是你無法在變量后面最加一些東西,就像

CFLAGS = $(CFLAGS) -O

因為它將導致一個變量擴展的無限的循環(實際上make會檢測到這個無限循環報告一個錯誤)。

另一個壞處是,在定義中所參照的任何函數(*note Functiosn for Transforming Text: Functions.)在每次變量擴展的時候都要執行。這導致make 運行緩慢。更糟糕的是,它導致通配符和shell 函數給出不確定的結果,因為你無法簡單地控制它們何時以何種方式被調用多少次。

為了防止上述的所有問題和不方便之處,這里有另外一個風格:簡單擴展變量。

"簡單擴展變量"由:=來定義(*note Setting Variables: Setting.),簡單擴展變量的值只被掃描一次,在任何其他的對此變量進行參照的變量和函數中擴展。簡單擴展變量的實際值就是你所寫的文本擴展結果。它不包含任何對其他變量的參照;它包含此變量被定義時的值。因此,

x := foo
y := $(x) bar
x := later

等價于:

y := foo bar

x := later

當一個簡單擴展變量被參照的時候,它的值會被逐字逐句替換。

This example also shows use of the variable
`MAKELEVEL', which is changed when it is passed down from level to
level. (*Note Communicating Variables to a Sub-`make':
Variables/Recursion, for information about `MAKELEVEL'.)

這里有一個更加復雜的例子,顯示了和shell 函數結合,對:=的利用。(*Note the 'shell' Function:Shell Function.)

ifeq (0,${MAKELEVEL})
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif

使用這種 :=的一個好處就是 典型的進入目錄的片段會看起來像這樣:?

${subdirs}:
${MAKE} -C $@ all

簡單擴展變量通常會使得復雜的makefile編程更精確,因為它們就像大多數的編程語言一樣進行工作。它們允許你用變量的值來重新定義自己(或者用某種方式在擴展函數中處理),可以更加高效地運行擴展函數(*note Functions for Transforming Text: Functions.)

你也可以用它們來在變量值中引入前導空格。在替換到變量參照和函數調用之前,前導空格會被忽略;這種方式下你可以通過利用變量參照來保護前導空格,像下面這樣:

nullstring :=
space := $(nullstring) # end of the line

這里變量space 的值是精確的一個空格。注釋'# end of the line' 在這里只是為了更加清晰地說明問題。因為末尾的空格字符不會從變量值中剝除,在行末的一個空格也可以達到同樣的效果(但是讀取來很困難)。

如果你在一個變量值得最后面附加一個空格,像上述那樣在此行后面附加一個注釋是一個好主意。相反地,如果你不想任何空格出現在你的變量末尾,你必須記住不要在行的末尾,在若干空格后面加一個注釋。就像這樣:

dir := /foo/bar # directory to put the frobs in

此時,dir 變量的值是 '/foo/bar '(末尾有四個空格),這可能不符合我們的預期。(想象比如 $(dir)/file這樣的東西)

這一由另外一個對變量的復制操作符號 ?=。這被常委一個條件變量夫之婦好,一位只有在一個變量尚未確定時它才有效果。下面的句子:

FOO ?= bar

等價于 (*note The `origin' Function: Origin
Function.):

ifeq ($(origin FOO), undefined)
FOO = bar
endif

請注意一個便來能夠設置為空值也相當于定義了,所以`?='不會設定給那個變量。
后文待續

轉載于:https://www.cnblogs.com/gaojian/archive/2012/10/03/2710745.html

總結

以上是生活随笔為你收集整理的GNU make manual 翻译( 一百五十五)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。