一文学完makefile语法
一、开始
1.Hello World
新建一个makefile文件,写入如下内容,
hello:
echo "Hello World"
clean:
echo "clean all"
执行make命令,输出结果为
echo "Hello World"
Hello World
2.makfile 语法
targets:prerequisites
command
...
target可以是一个 object file(目标文件),也可以是一个执行文件,还可以是一个标签(label)。prerequisites生成该 target 所依赖的文件和/或 target。command该 target 要执行的命令(任意的 shell 命令)。
这是一个文件的依赖关系,也就是说,target 这一个或多个的目标文件依赖于 prerequisites 中的文件,其生成规则定义在 command 中。说白一点就是说:prerequisites 中如果有一个以上的文件比 target 文件要新的话,command 所定义的命令就会被执行。
3.简单的例子
hello:hello.o
gcc hello.o -o hello # Runs third
hello.o:hello.c
gcc -c hello.c -o hello.o # Runs second
hello.c:
echo "int main() { return 0; }" > hello.c # Runs first
执行make或make hello,结果如下
echo "int main() { return 0; }" > hello.c # Runs first
gcc -c hello.c -o hello.o # Runs second
gcc hello.o -o hello # Runs third
可以看到执行的顺序与makefile内容是相反的,当执行make hello时,由于hello是由hello.o生成的,需要找到hello.o,但是hello.o是由hello.c生成的,那么需要先生成hello.c,以此类推,通过这个例子可以清晰地看到makefile是通过依赖关系自动推导的。
4.变量
files = file1 file2
some_file:$(files)
echo "look at this variable:" $(files)
file1:
touch file1
file2:
touch file2
clean:
rm -f file1 file2
makefile文件的变量不仅可以用
$(),也可以用${}。
二、目标文件
all: one two three
one:
touch one
two:
touch two
three:
touch three
clean:
rm -f one two three
这样写的话比较繁琐,可以使用$@,当一条规则有多个目标时,将为每个目标运行这些命令,$@是一个自动变量,包含目标名称。makefile可以重新写为
all: one two three
one two three:
echo $@
clean:
rm -r one two three
三、自动推导变量和通配符
1. 通配符 *
thing_wrong := *.o # Don't do this! '*' will not get expanded
thing_right := $(wildcard *.o)
all: one two three four
# Fails, because $(thing_wrong) is the string "*.o"
one: $(thing_wrong)
# Stays as *.o if there are no files that match this pattern :(
two: *.o
# Works as you would expect! In this case, it does nothing.
three: $(thing_right)
# Same as rule three
four: $(wildcard *.o)
*能够被用在target、prerequisites以及wildcard函数里。*直接用在变量中比较危险,不会被认为是通配符,而是被看做字符*。- 当
*没有被匹配到的时候,它就保持原样,比如像上面two中的*.o能够匹配到后缀为.o的文件,但是如果没有匹配到的话,会被看做是普通的字符串*.o,这样是比较危险的。
2. 通配符 %
- 当在 "匹配 "模式下使用时,它匹配字符串中的一个或多个字符。这种匹配被称为stem。
- 当在 "替换 "模式下使用时,它获取被匹配的stem并将其替换到一个字符串中。
%最常被用于规则定义和一些特定的功能中。在下文中会被重点说明。
3. 自动推导变量
$@:代表目标文件(target)$^:代表所有的依赖文件(prerequisites)$<:代表第一个依赖文件(prerequisites中最左边的那个)。$?:代表示比目标还要新的依赖文件列表。以空格分隔。$%:仅当目标是函数库文件中,表示规则中的目标成员名。
hey: one two
# Outputs "hey", since this is the first target
echo $@
# Outputs all prerequisites newer than the target
echo $?
# Outputs all prerequisites
echo $^
touch hey
one:
touch one
two:
touch two
clean:
rm -f hey one two
第一个echo $@输出的是hey,即目标文件。echo $?和echo $^的输出看起来没啥区别,但是调换一下次序,如
hey: one two
echo $^
echo "---------------"
echo $?
echo "---------------"
touch hey
one:
touch one
two:
touch two
clean:
rm -f hey one two
输出
touch one
touch two
echo one two
one two
echo "---------------"
---------------
echo one two
one two
echo "---------------"
---------------
touch hey
可以明显地看到echo $?没有调用touch one和touch two,原因是echo $^已经调用并生成最新文件,不需要再次调用。
四、高效技能
1. 静态模式规则
targets ...: target-pattern: prereq-patterns ...
commands
其本质是,给定的目标被目标模式(通过%通配符)匹配。被匹配的内容被称为stem。然后,该stem被替换到前置条件模式中,以生成目标的前置条件。
objects = foo.o bar.o all.o
all: $(objects)
# These files compile via implicit rules
foo.o: foo.c
bar.o: bar.c
all.o: all.c
all.c:
echo "int main() { return 0; }" > all.c
%.c:
touch $@
clean:
rm -f *.c *.o all
更有效地方式可以这样写
objects = foo.o bar.o all.o
all: $(objects)
# These files compile via implicit rules
# Syntax - targets ...: target-pattern: prereq-patterns ...
# In the case of the first target, foo.o, the target-pattern matches foo.o and sets the "stem" to be "foo".
# It then replaces the '%' in prereq-patterns with that stem
$(objects): %.o: %.c
all.c:
echo "int main() { return 0; }" > all.c
%.c:
touch $@
clean:
rm -f *.c *.o all
2. 静态模式规则和过滤器
obj_files = foo.result bar.o lose.o
src_files = foo.raw bar.c lose.c
all: $(obj_files)
# 只会让.o的文件执行
$(filter %.o,$(obj_files)): %.o: %.c
echo "target: $@ prereq: $<"
# 只会让.result的文件执行
$(filter %.result,$(obj_files)): %.result: %.raw
echo "target: $@ prereq: $<"
%.c %.raw:
touch $@
clean:
rm -f $(src_files)
3. 隐式规则
CC = gcc # Flag for implicit rules
CFLAGS = -g # Flag for implicit rules. Turn on debug info
# Implicit rule #1: blah is built via the C linker implicit rule
# Implicit rule #2: blah.o is built via the C compilation implicit rule, because blah.c exists
# Define a pattern rule that compiles every .c file into a .o file
blah:blah.o
%.o : %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
blah.c:
echo "int main() { return 0; }" > blah.c
clean:
rm -f blah*
4. 双引号规则
双冒号规则很少使用,但允许为同一目标定义多个规则。如果这些是单冒号,就会打印出一个警告,而且只有第二组命令会运行。
all: blah
blah::
echo "hello"
blah::
echo "hello again"
五、命令与执行
在命令前添加@以阻止其被打印出来,也可以在运行make -s,效果好比在每一行前添加一个@。
all:
@echo "This make line will not be printed"
echo "But this will"
执行make输出
This make line will not be printed
echo "But this will"
But this will
执行make -s时,输出
This make line will not be printed
But this will
每条命令都在一个新的shell中运行(或者至少效果是这样)。
all:
cd ..
# The cd above does not affect this line, because each command is effectively run in a new shell
echo `pwd`
# This cd command affects the next because they are on the same line
cd ..;echo `pwd`
# Same as above
cd ..; \
echo `pwd`
在执行make时,增加参数-k,以便在出现错误时继续运行。如果你想一次性看到make的所有错误,这很有帮助。
在命令前加一个-来抑制错误的发生,如果在make中添加-i参数,可以使每条命令都出现这种情况。
one:
# This error will be printed but ignored, and make will continue to run
false
touch one
执行make,会出现出现终止程序错误
false
make: *** [one] Error 1
如果在false前面加上-,输出
false
make: [one] Error 1 (ignored)
touch one
注意:如果你在
make过程中ctrl+c,会删除刚刚制作的较新目标文件。
要递归调用一个makefile,请使用特殊的$(MAKE)而不是make。
all:
touch two
cd subdir && $(MAKE)
clean:
rm -rf subdir two
在当前文件夹内新建一个名为subdir的子文件夹,makefile的内容为
hello:
touch one
export指令获取一个变量,并使其能够被调用的make命令访问。在这个例子中,corey被export,这样subdir中的makefile就可以使用它。
new_contents = "hello:\n\\techo \$$(cooly)"
all:
mkdir -p subdir
echo $(new_contents) | sed -e 's/^ //' > subdir/makefile
@echo "---MAKEFILE CONTENTS---"
@cd subdir && cat makefile
@echo "---END MAKEFILE CONTENTS---"
cd subdir && $(MAKE)
# Note that variables and exports. They are set/affected globally.
cooly = "The subdirectory can see me!"
export cooly
# This would nullify the line above: unexport cooly
clean:
rm -rf subdir
export定义的变量也可以在本makefile中使用
one=this will only work locally
export two=we can run subcommands with this
all:
@echo $(one)
@echo $$one # 没有输出
@echo $(two)
@echo $$two # 等效于$(two)
输出
this will only work locally
we can run subcommands with this
we can run subcommands with this
在makefile中的变量前输入.EXPORT_ALL_VARIABLES:,可以等效与在变量前输入export。
.EXPORT_ALL_VARIABLES:
new_contents = "hello:\n\techo \$$(cooly)"
cooly = "The subdirectory can see me!"
# This would nullify the line above: unexport cooly
all:
mkdir -p subdir
echo $(new_contents) | sed -e 's/^ //' > subdir/makefile
@echo "---MAKEFILE CONTENTS---"
@cd subdir && cat makefile
@echo "---END MAKEFILE CONTENTS---"
cd subdir && $(MAKE)
clean:
rm -rf subdir
六、再谈变量
1. 变量符号类别
当使用=时,该变量使用的时候会全局搜索,不需要提前定义。
当使用:=是,需要提前定义,否者找不到,用空字符串代替。
# Recursive variable. This will print "later" below
one = one ${later_variable}
# Simply expanded variable. This will not print "later" below
two := two ${later_variable}
later_variable = later
three := three ${later_variable}
all:
echo $(one)
echo $(two)
echo $(three)
输出
echo one later
one later
echo two
two
echo three later
three later
?=表示如果先前没有对该变量赋值,那么会赋值成功,否者没有效果。
one = hello
one ?= will not be set
two ?= will be set
all:
echo $(one)
echo $(two)
输出
echo hello
hello
echo will be set
will be set
行末的空格不会被忽略,但行首的空格会被忽略。要使一个变量带有一个空格,可以使用空字符串变量
with_spaces = hello # with_spaces has many spaces after "hello"
after = $(with_spaces)there
nullstring =
space = $(nullstring) # Make a variable with a single space.
all:
echo "$(after)"
echo "$(space)"start"$(space)"end
输出
echo "hello there"
hello there
echo " "start" "end
start end
未定义的变量被认为是空字符串
+=表示追加(append)
foo := start
foo += more
all:
echo $(foo)
输出
echo start more
start more
make有两个比较特殊的变量:SHELL”和“ MAKEFLAGS”,这两个变量除非使用“ unexport”声明,否则的话在整个 make的执行过程中,它们的值始终自动的传递给子make。
2. 命令行重载
用于指示makefile中定义的变量不能被覆盖,变量的定义和赋值都需要使用override关键字。
override one = did_override
one = hello
two = world
all:
echo $(one)
echo $(two)
输出为
echo did_override
did_override
echo world
world
3. 宏定义
用define...endef包起来,
define one
echo hello world
endef
all:
@$(one)
输出
hello world
但是在define...endef里的每一行都是单独执行的,
define one
export str=hello
echo $$str
endef
all:
@$(one)
这样的话会输出空字符串,因为他们没有联系。makefile允许多目标,当执行多目标的时候,会按照顺序全部执行。
all: one = cool
all:
echo $(one)
other:
echo $(one)
make all有输出cool,make other没有输出。
你可以为特定的目标模式分配变量
%.c: one = cool
blah.c:
echo one is defined: $(one)
other:
echo one is nothing: $(one)
只有执行make blah.c时,才会匹配到第一行。
七、条件
if/else语句
foo = ok
all:
ifeq ($(foo),ok)
echo "foo equal ok"
else
echo "nope"
endif
判断一个变量是否为空
nullstring =
foo = $(nullstring) # end of line; there is a space here
all:
ifeq ($(strip $(foo)),)
echo "foo is empty after being stripped"
endif
ifeq ($(nullstring),)
echo "nullstring doesn't even have spaces"
endif
判断变量是否被定义(define),ifdef只查看是否有定义的东西。
bar =
foo = $(bar)
all:
ifdef foo
echo "foo is defined"
endif
ifdef bar
echo "but bar is not"
endif
输出为
echo "foo is defined"
foo is defined
八、函数
1. 替换函数
函数的形式有${fn,argument}和$(fn,argument)两种,先看一个字符串替换函数
bar := ${subst not,totally,"I am not superman"}
all:
@echo $(bar)
输出
I am totally superman
comma := ,
empty:=
space := $(empty) $(empty)
foo := a b c
bar := $(subst $(space),$(comma),$(foo))
all:
@echo $(bar)
输出
a,b,c
2. 字符串置换
foo := a.o b.o l.a c.o
one := $(subst %.o,%.c,$(foo))
# This is a shorthand for the above
two := $(foo:%.o=%.c)
# This is the suffix-only shorthand, and is also equivalent to the above.
three := $(foo:.o=.c)
all:
echo $(one)
echo $(two)
echo $(three)
输出
echo a.c b.c l.a c.c
a.c b.c l.a c.c
echo a.c b.c l.a c.c
a.c b.c l.a c.c
echo a.c b.c l.a c.c
a.c b.c l.a c.c
3. foreach 函数
foo := who are you
# For each "word" in foo, output that same word with an exclamation after
bar := $(foreach wrd,$(foo),$(wrd)!)
all:
# Output is "who! are! you!"
@echo $(bar)
4. if 函数
foo := $(if this-is-not-empty,then!,else!)
empty :=
bar := $(if $(empty),then!,else!)
all:
@echo $(foo) # Output then!
@echo $(bar) # Output else!
5. call 函数
sweet_new_fn = Variable Name: $(0) First: $(1) Second: $(2) Empty Variable: $(3)
all:
# Outputs "Variable Name: sweet_new_fn First: go Second: tigers Empty Variable:"
@echo $(call sweet_new_fn, go, tigers)
6. shell 函数
all:
@echo $(shell ls -la) # Very ugly because the newlines are gone!
九、 其他特性
1. include
include指令告诉make读取一个或多个其他makefile文件。它是makefile文件中的一行,看起来像这样。
include filenames...
2. vpath 指令
使用vpath来指定某些先决条件的存在位置。格式是vpath <pattern> <directories, space/colon separated>。<pattern>可以有一个%,它可以匹配任何零或更多的字符。你也可以用变量VPATH在全局范围内这样做。
vpath %.h ./headers ./other-directory
some_binary: ./headers blah.h
touch some_binary
./headers:
mkdir ./headers
blah.h:
touch ./headers/blah.h
clean:
rm -rf ./headers
rm -f some_binary
3. 多行
可以用\进行分割多行
some_file:
echo This line is too long, so \
it is broken up into multiple lines
4. .phony
在目标上添加.PHONY可以防止make将虚假的目标与文件名混淆。在这个例子中,如果文件clean被创建,make clean仍然会被运行。
some_file:
touch some_file
touch clean
.PHONY: clean
clean:
rm -f some_file
rm -f clean
5. .delete_on_error
如果一个命令返回非零的退出状态,make工具将停止运行一个规则(并将传播回先决条件)。
如果规则以这种方式失败,DELETE_ON_ERROR将删除该规则的目标。这将发生在所有的目标上,而不是像PHONY那样只发生在它之前的那个目标。始终使用这个是个好主意,即使make由于历史原因没有这样做。
.DELETE_ON_ERROR:
all: one two
one:
touch one
false
two:
touch two
false
输出
touch one
false
make: *** [makefile:21:one] 错误 1
make: *** 正在删除文件“one”
一文学完makefile语法的更多相关文章
- makefile语法
makefile很重要 什么是makefile? 或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional 的程 ...
- 【转】makefile语法规则
1.源程序的编译 在Linux下面,如果要编译一个C语言源程序,我们要使用GNU的gcc编译器. 下面我们以一个实例来说明如何使用gcc编译器. 假设我们有下面一个非常简单的 ...
- 我所不知道的Makefile语法
问题一: $(CC) -c $^ -o $(ROOT_DIR)/$(OBJS_DIR)/$@ 这里的$^和$@是设么意思? 经过查找,该特殊符号的用法如下: 假如:all:library.cpp ma ...
- makefile 语法笔记 3
这里说明了 在一些情况下 这也是可以使用通配符的 objects =*.o 这种情况是不会展开的 makefile 中的变量是C++/C 中的宏 如果希望展开,可以使用 $(wildcard *.o) ...
- 一文学完Linux常用命令
一.Linux 终端命令格式 1.终端命令格式 完整版参考链接:Linux常用命令完整版 command [-options] [parameter] 说明: command : 命令名,相应功能的英 ...
- 一文学完Linux Shell编程,比书都好懂
一. Shell 编程 1. 简介 Shell 是一个用 C 语言编写的程序,通过 Shell 用户可以访问操作系统内核服务. Shell 既是一种命令语言,又是一种程序设计语言. Shell scr ...
- vscode vue template 下 style 的样式自动提示 #bug 这个搞完vue语法esLint就又不好使了,ERR
网上都是 "*.vue": "vue",改成"*.vue": "html" 就ok了 "files.ass ...
- Makefile研究 (一)—— 必备语法
摘自:http://blog.csdn.net/jundic/article/details/17535445 参考文档:http://blog.csdn.net/wrx1721267632/arti ...
- 羽夏 MakeFile 简明教程
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.该文章根据 GNU Make Manual 进行汉化处理并作出自己的整理,一是我对 Make 的学习记录,二是对大家学习 MakeF ...
随机推荐
- 3步安装Python虚拟环境virtualenv
1. pip安装必要库 pip install virtualenv -i https://pypi.douban.com/simple pip install virtualenvwrapper - ...
- 【PHP】用了这么久的Laravel框架,你分析过核心架构了没
Laravel最初的设计是为了面向MVC架构的,它可以满足如事件处理.用户身份验证等各种需求.另外它还有一个由管理数据库强力支持,用于管理模块化和可扩展性代码的软件包管理器. Laravel以其简洁. ...
- OOUnit1Summary
一.前三次作业内容分析 前言 第一单元的作业以表达式求导为主题,分三次要求逐步增加,难度逐步提高.这三次作业下来,本人既有收获,也有遗憾,因此通过接下来的内容对我这三次作业进行分析和总结,希望能能为我 ...
- hello world!goodbye world~
我有个朋友,做ios开发做了5年,年前回家转行赚大钱去了,这个标题,其实就是因他而生. 我本人做的.net开发,也差不多快5年时间了,在这个时候暂借博客园这个平台说几句心里话,骚了勿喷:) 其实我是个 ...
- unzip解压中文乱码
1 问题描述 直接 unzip xxx.zip 乱码,肯定是编码问题了不用问.但是unzip没有指定编码的选项: 网上的解决方案如下: unzip -O GBK/GB18030CP936 xx.zip ...
- 8. vue给标签动态绑定title
在利用vue开发时,如果标签宽度比较小,我们需要利用overflow:hidden;text-overflow:ellipsis;white-space: nowrap;对其进行隐藏,但隐藏后如何读其 ...
- 15. Vue2.4+新增属性.sync
.sync 在vue2.4以前,父组件向子组件传值用props:子组件不能直接更改父组件传入的值,需要通过$emit触发自定义事件,通知父组件改变后的值. 父组件: <template> ...
- 为什么 DNS 协议使用 UDP?只使用了 UDP 吗?
尽人事,听天命.博主东南大学硕士在读,携程 Java 后台开发暑期实习生,热爱健身和篮球,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收 ...
- Prime Ring Problem UVA - 524
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,...,n int ...
- 记canvas画笔笔迹的多次优化过程
我们的项目是面向学校老师的教学软件,所以肯定少不了互动白板的功能,而这个里面的画笔功能是由我来开发的,下面介绍这个过程中遇到的问题以及解决方法. 首先给大家明确下由于软件中的画布可以自由移动,会超出屏 ...