继续翻译
For example, you might have a list of object files: objects = foo.o bar.o baz.o To get the list of corresponding source files, you could simply write: $(objects:.o=.c) instead of using the general form: $(patsubst %.o,%.c,$(objects)) `$(strip STRING)' Removes leading and trailing whitespace from STRING and replaces each internal sequence of one or more whitespace characters with a single space. Thus, `$(strip a b c )' results in `a b c'. The function `strip' can be very useful when used in conjunction with conditionals. When comparing something with the empty string `' using `ifeq' or `ifneq', you usually want a string of just whitespace to match the empty string (*note Conditionals::). Thus, the following may fail to have the desired results: .PHONY: all ifneq "$(needs_made)" "" all: $(needs_made) else all:;@echo 'Nothing to make!' endif Replacing the variable reference `$(needs_made)' with the function call `$(strip $(needs_made))' in the `ifneq' directive would make it more robust. `$(findstring FIND,IN)' Searches IN for an occurrence of FIND. If it occurs, the value is FIND; otherwise, the value is empty. You can use this function in a conditional to test for the presence of a specific substring in a given string. Thus, the two examples, $(findstring a,a b c) $(findstring a,b c) produce the values `a' and `' (the empty string), respectively. *Note Testing Flags::, for a practical application of `findstring'. `$(filter PATTERN...,TEXT)' Returns all whitespace-separated words in TEXT that _do_ match any of the PATTERN words, removing any words that _do not_ match. The patterns are written using `%', just like the patterns used in the `patsubst' function above. The `filter' function can be used to separate out different types of strings (such as file names) in a variable. For example: sources := foo.c bar.c baz.s ugh.h foo: $(sources) cc $(filter %.c %.s,$(sources)) -o foo says that `foo' depends of `foo.c', `bar.c', `baz.s' and `ugh.h' but only `foo.c', `bar.c' and `baz.s' should be specified in the command to the compiler.
例如,你可以有一个目标文件的列表:
objects = foo.o bar.o baz.o
为了得到相应的源文件的列表,你可以简单地写:
$(objects:.o=.c)
而不是使用:
$(patsubst %.o,%.c,$(objects))
`$(strip STRING)'
从STRING 里面去除前面的和后面的空格,并且将内在的一个或多个空格序列替换为一个单独的空格。因此,$(strip a b c) 的结果是 a b c。
strip 函数当何条件式结合的时候,可以非常游泳。当和空字符串比较时用 ifeq 或者 ifneq,你通常想要几个只有空格的字符串来匹配空字符串(*note Conditionals::)。
因此,下列的可能会无法达成想要的结果:
.PHONY: all
ifneq "$(needs_made)" ""
all: $(needs_made)
else
all:;@echo 'Nothing to make!'
endif
在ifneq 指令中把变量参照 $(needs_made)替为函数调用 $(strip $(needs_made))可能会更加的健壮。
以下一段,因故暂停。
`$(findstring FIND,IN)'
Searches IN for an occurrence of FIND. If it occurs, the value is
FIND; otherwise, the value is empty. You can use this function in
a conditional to test for the presence of a specific substring in
a given string. Thus, the two examples,
$(findstring a,a b c)
$(findstring a,b c)
produce the values `a' and `' (the empty string), respectively.
*Note Testing Flags::, for a practical application of `findstring'.
`$(filter PATTERN...,TEXT)'
Returns all whitespace-separated words in TEXT that _do_ match any
of the PATTERN words, removing any words that _do not_ match. The
patterns are written using `%', just like the patterns used in the
`patsubst' function above.
The `filter' function can be used to separate out different types
of strings (such as file names) in a variable. For example:
sources := foo.c bar.c baz.s ugh.h
foo: $(sources)
cc $(filter %.c %.s,$(sources)) -o foo
says that `foo' depends of `foo.c', `bar.c', `baz.s' and `ugh.h'
but only `foo.c', `bar.c' and `baz.s' should be specified in the
command to the compiler.
后文待续