以GNU make为例
makefile原理:
makefile组成部分:
变量
内置变量:
VPATH: Search Path for All Prerequisites
The value of the make variable VPATH specifies a list of directories that make should search.
Most often, the directories are expected to contain prerequisite files that are not in the
current directory; however, make uses VPATH as a search list for both prerequisites and
targets of rules.
In the VPATH variable, directory names are separated by colons or blanks.
For example,
VPATH = src:../headers
specifies a path containing two directories, src and ../headers, which make searches in
that order.
函数
函数调用语法:
$(function arguments)
${function arguments}
text function: filter,filter-out
The call function is unique in that it can be used to create new parameterized functions.
You can write a complex expression as the value of a variable, then use call to expand it with different values.
The syntax of the call function is:
$(call variable,param,param,...)
When make expands this function, it assigns each param to temporary variables $(1),$(2), etc. The variable $(0) will contain variable.
示例1
comma:= ,
empty:=
space:= $(empty) $(empty)
foo:= a b c
bar:= $(subst $(space),$(comma),$(foo))
# bar is now ‘a,b,c’.
pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
LS := $(call pathsearch,ls)
指令:
条件指令:ifeq,ifneq,ifdef,ifndef
export:Tell make to export all variables to child processes by default.
示例1
libs_for_gcc = -lgnu
normal_libs =
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
else
$(CC) -o foo $(objects) $(normal_libs)
endif
示例1
ifeq ($(strip $(foo)),) #去除变量首尾空白字符,将中间连续的空白字符变成一个
text-if-empty
endif
=/:=/?=
variable assignment operator, because it only has an effect if the variable is not yet defined.
This statement: