跟我一起学Makefile--陈浩
跟我一起写 Makefile 作者:陈皓
第一部分、概述...............................................................................................................................6
第二部分、关于程序的编译和链接...............................................................................................6
第三部分、Makefile 介绍............................................................................................................7
一、Makefile的规则..............................................................................................................7
二、一个示例...........................................................................................................................8
三、make是如何工作的........................................................................................................9
四、makefile中使用变量....................................................................................................10
五、让make自动推导..........................................................................................................11
六、另类风格的makefile....................................................................................................12
七、清空目标文件的规则.....................................................................................................13
第四部分、Makefile 总述..........................................................................................................13
一、Makefile里有什么?....................................................................................................13
1、显式规则。..............................................................................................................14
2、隐晦规则。..............................................................................................................14
3、变量的定义。..........................................................................................................14
4、文件指示。..............................................................................................................14
5、注释。......................................................................................................................14
二、Makefile的文件名........................................................................................................15
三、引用其它的Makefile....................................................................................................15
四、环境变量 MAKEFILES................................................................................................16
五、make的工作方式..........................................................................................................16
第五部分、书写规则.....................................................................................................................17
一、规则举例.........................................................................................................................17
二、规则的语法.....................................................................................................................17
三、在规则中使用通配符.....................................................................................................18
四、文件搜寻.........................................................................................................................19
五、伪目标.............................................................................................................................20
六、多目标.............................................................................................................................22
七、静态模式.........................................................................................................................22
八、自动生成依赖性.............................................................................................................24
第六部分 书写命令.......................................................................................................................25
一、显示命令.........................................................................................................................26
二、命令执行.........................................................................................................................26
三、命令出错.........................................................................................................................27
四、嵌套执行make..............................................................................................................28
五、定义命令包.....................................................................................................................30
第七部分 使用变量.......................................................................................................................30
一、变量的基础.....................................................................................................................31
二、变量中的变量.................................................................................................................32
三、变量高级用法.................................................................................................................34
四、追加变量值.....................................................................................................................37
五、override 指示符..........................................................................................................37
六、多行变量.........................................................................................................................38
第 2 页共 78 页 2005 年 10 月 14 日整理:祝冬华
跟我一起写 Makefile 作者:陈皓
七、环境变量.........................................................................................................................38
八、目标变量.........................................................................................................................39
九、模式变量.........................................................................................................................40
第八部分 使用条件判断...............................................................................................................40
一、示例.................................................................................................................................40
二、语法.................................................................................................................................42
第九部分 使用函数.......................................................................................................................43
一、函数的调用语法.............................................................................................................44
二、字符串处理函数.............................................................................................................44
1、subst.......................................................................................................................44
2、patsubst.................................................................................................................45
3、strip.........................................................................................................................45
4、findstring...............................................................................................................46
5、filter.........................................................................................................................46
6、filter-out.................................................................................................................46
7、sort..........................................................................................................................47
8、word........................................................................................................................47
9、wordlist ..................................................................................................................47
10、words....................................................................................................................47
11、firstword..............................................................................................................48
12、字符串函数实例....................................................................................................48
三、文件名操作函数.............................................................................................................48
1、dir.............................................................................................................................48
2、notdir......................................................................................................................48
3、suffix.......................................................................................................................49
4、basename..............................................................................................................49
5、addsuffix................................................................................................................49
6、addprefix...............................................................................................................49
7、join...........................................................................................................................50
四、foreach 函数................................................................................................................50
五、if 函数............................................................................................................................50
六、call函数..........................................................................................................................51
七、origin函数.....................................................................................................................51
“undefined”.................................................................................................................52
“default”.......................................................................................................................52
“file”...............................................................................................................................52
“command line”.........................................................................................................52
“override” ....................................................................................................................52
“automatic”.................................................................................................................52
八、shell函数 .......................................................................................................................53
九、控制make的函数..........................................................................................................53
1、error........................................................................................................................53
2、warning..................................................................................................................54
第十部分 make 的运行..............................................................................................................54
第 3 页共 78 页 2005 年 10 月 14 日整理:祝冬华
跟我一起写 Makefile 作者:陈皓
一、make的退出码..............................................................................................................54
二、指定Makefile................................................................................................................54
三、指定目标.........................................................................................................................55
“all”................................................................................................................................56
“clean” ..........................................................................................................................56
“install”.........................................................................................................................56
“print” ...........................................................................................................................56
“tar”...............................................................................................................................56
“dist”..............................................................................................................................56
“TAGS”..........................................................................................................................56
“check”和“test”..........................................................................................................56
四、检查规则.........................................................................................................................57
五、make的参数..................................................................................................................57
第十一部分 隐含规则...................................................................................................................61
一、使用隐含规则.................................................................................................................61
二、隐含规则一览.................................................................................................................62
1、编译C程序的隐含规则 ...........................................................................................63
2、编译C 程序的隐含规则.....................................................................................63
3、编译Pascal程序的隐含规则..................................................................................63
4、编译Fortran/Ratfor程序的隐含规则..................................................................63
5、预处理Fortran/Ratfor程序的隐含规则..............................................................63
6、编译Modula-2 程序的隐含规则...........................................................................63
7、汇编和汇编预处理的隐含规则 ..............................................................................64
8、链接Object文件的隐含规则.................................................................................64
9、Yacc C程序时的隐含规则 .....................................................................................64
10、Lex C程序时的隐含规则.....................................................................................64
11、Lex Ratfor程序时的隐含规则...........................................................................65
12、从C程序、Yacc文件或Lex文件创建Lint库的隐含规则..................................65
三、隐含规则使用的变量.....................................................................................................65
1、关于命令的变量。..................................................................................................65
2、关于命令参数的变量..............................................................................................66
四、隐含规则链.....................................................................................................................67
五、定义模式规则.................................................................................................................68
1、模式规则介绍..........................................................................................................68
2、模式规则示例..........................................................................................................69
3、自动化变量..............................................................................................................70
4、模式的匹配..............................................................................................................72
5、重载内建隐含规则..................................................................................................72
六、老式风格的"后缀规则".................................................................................................73
七、隐含规则搜索算法.........................................................................................................74
第十二部分 使用make更新函数库文件....................................................................................75
一、函数库文件的成员.........................................................................................................75
二、函数库成员的隐含规则.................................................................................................75
三、函数库文件的后缀规则.................................................................................................76
第 4 页共 78 页 2005 年 10 月 14 日整理:祝冬华
跟我一起写 Makefile 作者:陈皓
四、注意事项.........................................................................................................................76
第十三部分 后序...........................................................................................................................77
评论