Makefile实战:构建计算器项目与库管理

需积分: 1 0 下载量 83 浏览量 更新于2024-06-17 1 收藏 406KB PDF 举报
"Makefile工程实战.pdf" 涵盖了从基础到实践的Makefile应用,通过构建一个简单的计算器项目,展示了如何使用Makefile管理编译过程,包括生成静态库和动态库,以及多目录文件管理。教程详细讲解了以下内容: 1. **构建工程**:首先创建一个名为`operation`的目录,包含加法模块(add.c和add.h)和主文件(operation.c),主文件调用各个模块进行计算。 2. **自动添加目标对头文件的依赖**:Makefile中的`%.h`规则确保了当头文件改变时,依赖于这些头文件的目标文件会被重新编译。 3. **目录管理源文件**:通过在Makefile中指定各个源文件的位置,便于管理和编译。 4. **目录管理目标文件**:Makefile的`OBJ`变量列出了所有需要生成的目标文件,便于统一管理和链接。 5. **目录管理依赖文件**:利用`%.o:%.c`规则,自动处理源文件到目标文件的转换,确保正确跟踪依赖关系。 6. **目录管理头文件**:通过`%.h`规则,确保头文件的更改能触发相关目标的重建。 7. **静态库的生成和使用**:教程将展示如何创建静态库(.a文件),并将其链接到项目中。 8. **动态库的生成和使用**:同样,动态库(.so文件)的生成和加载也是Makefile管理的一部分。 9. **第三方库文件的使用**:讲解如何在Makefile中集成和链接外部库,以利用第三方库的功能。 10. **重构Makefile**:最后,通过不断优化和调整Makefile,形成一个通用的模板,适用于不同项目的需求。 在Makefile中,`$@`代表目标文件,`$^`代表所有依赖文件。`clean`是一个伪目标,用于清理编译过程中产生的临时目标文件和最终的可执行文件。`%.o:%.c`规则定义了源文件到目标文件的编译过程,`gcc -o $@ -c $^`表示将源文件编译成目标文件,并链接成可执行文件。 通过这个实战项目,读者将深入理解Makefile在软件开发中的作用,学会如何编写和优化Makefile,以便更高效地管理和构建复杂的工程。
384 浏览量
第一部分、概述...............................................................................................................................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