Makefile 总结

以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


其他

=/:=/?=

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:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值