第七章 多模块软件的编译和链接.ppt

上传人:本田雅阁 文档编号:2121021 上传时间:2019-02-18 格式:PPT 页数:44 大小:813.01KB
返回 下载 相关 举报
第七章 多模块软件的编译和链接.ppt_第1页
第1页 / 共44页
第七章 多模块软件的编译和链接.ppt_第2页
第2页 / 共44页
第七章 多模块软件的编译和链接.ppt_第3页
第3页 / 共44页
亲,该文档总共44页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《第七章 多模块软件的编译和链接.ppt》由会员分享,可在线阅读,更多相关《第七章 多模块软件的编译和链接.ppt(44页珍藏版)》请在三一文库上搜索。

1、多模块软件的编译和链接,第7章,预习检查,$make f 文件,是什么文件? 遵循makefile语法的文件,也就是makefile文件 目标列表: 关联性列表,含义是什么? 目标列表依赖于关联性列表 简单变量的定义方式? 变量名 := 文本 举一个make内置变量的例 CFLAGS, $,$?等 make clean,clean常见的含义是什么? clean是虚目标,删除make生成的文件,2,本章目标,了解Make实用程序的功能 使用makefile管理多模块软件 掌握makefile的规则,变量 掌握makefile的虚目标规则 了解autoconf的原理和用法,本章结构,简介,多模块软

2、件的编译和链接,Makefile和Make Rules,默认规则,虚目标,特殊目标,一般性语法错误及其纠正措施,autoconf,多模块软件、依赖树和make,Make实用程序,make实用程序对简单变量的支持,内建变量,命令行的使用和调试,1-1 多模块软件,实用的软件都是拥有多个源文件 这些源文件称之为模块 多模块软件 多模块软件,2-1make,一个差强人意的办法 使用shell脚本 上述的缺点,导致了make的产生。,$cat build.sc gcc c prog1.c prog2.c prog3.c gcc o prog prog1.o prog2.o prog3.o,2-1mak

3、e,Make的产生,2-1make,管理多模块程序的编译和连接 读取一个说明文件-makefile 描述系统中各模块的依赖关系 Make使重编译的次数达到最小化 Makefile描述的依赖关系 各组件文件的时间戳 Makefile 实质上是一种脚本语言,2-1Make,2-1make,2-1makefile,目标列表: 关联性列表 命令列表,目标列表: 关联性列表; 命令列表,也称为先决条件,2-1makefile,注释 # 连接符 关联列表和命令列表中使用shell通配符 ? * ,与shell脚本的相同,2-1makefile,实例,源码-power.c #include main() f

4、loat x,y; printf(“the program take x and from stdin and displays xy.n”); printf(“enter number x:”); scanf(“%f”, ,$cat Makefile Sample makefile for the power program Remember:each command line starts with a TAB power:power.c gcc power.c -o power lm $,制表符,2-1makefile,当目标文件比关联文件更新 更新关联文件,对比,$make make:

5、poweris up to date,$touch powerC $make gcc power.c o power lm,仅仅只更新文件的修改时间为当前时间,目标文件存在,且比关联文件更新,重新编译更新的关联文件,2-2依赖树,把power.c分成两个文件,#cat power.c #include double compute(double x,double y); main() float x,y; printf(”The program takes x from stdin and displays xy.n”); printf(”Enter number x:”); scanf(”f

6、,&x); printf(”Enter number y:”); scanf(”f,&y); printf(”xy is:63fn”, compute(x,y); ,#cat compute.c #include double compute(double x,double y) return(pow(double)x,(double)y); ,2-2依赖树,power.o,compute.o,power:power.ocompute.o gcc power.o compute.o -o power-lm,2-2依赖树,power.o,compute.o,power:power.ocomput

7、e.o gcc power.o compute.o -o power-lm power.o:power.c gcc c power.c compute.o:compute.c gcc c compute.c,power.c,compute.c,$make gcc-c powerc gcc-c computec gee powero computeo-o powerlm,树中节点的处理是自底向上的,由叶结点的符节点开始,2-2依赖树,进一步分成六个文件,建立依赖树,$cat computec include include”computeh” double compute(double x,do

8、uble y) return(pow(double)x,(double)y);,$cat main.h /*Declaration of prompts to users */ const char *PROMPTl=”Enter the value of x:” const char *PROMPT2=”Enter the value of y:”,$cat inputC # include”inputh” double input(const char *s) floatx; printf(”s”,s); scanf(“f”,&x); return(x); ,$cat input.h /*

9、 Declaration of the”input”function*/ double input(char*);,cat computeh /* Declaration of the“compute”function*/ double compute(double,double);,2-2依赖树,$cat mainc #include #include”mainh” #include”computeh” #include”inputh” main() double x,y; printf(”The program takes X and Y from stdin and displays x

10、y.n”); x=input(PROMPTl); y=input(PROMPT2); printf(”xy is:6.3fn”,compute(x,y); ,2-2依赖树,$ cat makefile power:main.o input.o compute.o gcc main.o input.o compute.o -o power -1m main.o:main.c main.h input.h compute.h gcc -c main.c input.o:input.c input.h gcc -c input.c compute.o:compute.c computeh gcc -

11、c compute.c $,$ make gcc-c main.c gcc-c input.c gcc -c compute.c gcc main.o input.o compute.o -o power -1m,1.第一个先决条件不存在,或者先决条件作为目标文件的先决条件更新。生成第一个 2.当所有的先决条件更新后,生成最终目标文件,2-3默认模式规则,-默认的后缀规则 SUFFIXES:ocs .c.o: $(CC)$(CFLAGS)-c $ .s.o: $(AS)$(ASFLAGS) -o $ $,-默认的模式规则(gnu Make) %.o:%.c: $(CC)$(CFLAGS)-c

12、$ %.o:%.s $(AS)$(ASFLAGS) -o $ $,-利用默认的规则修改后的makefile $cat makefile power:main.o input.o compute.o gcc main.o input.o compute.o -o power -lm main.o:main.h input.h compute.h input.o:input.h compute.o:compute.h,内置变量,以后会经常遇到,阶段总结,为什么使用make? Makefile的语法规则 依赖树的分析 默认的模式规则,2-4简单变量,简单变量 定义: 变量名 :=文本 添加: 变量名

13、 += 文本 引用 $(变量名) $变量名 $单字符变量,C=gcc $C,见过其他的模式吗? 变量名 = 文本 变量名 ?= 文本 超出了本章的返回,2-4简单变量,$cat makefile CC := gcc OPTIONS := -O3 OBJECTS := main.o OBJECTS += input.o compute.o SOURCES := main.c inputc compute.c HEADERS := main.h input.h compute.h power:$(OBJECTS) $(CC)$(OPTIONS) -o power $(OBJECTS) -lm ma

14、in.o:mainh inputh computeh input.o:inputh compute.o:computeh power.tar:makefile $(HEADERS) $(SOURCES) tar -cvf power.tar makefile $(HEADERS) $(SOURCES) clean: rm *.o $,2-5 内置变量,2-5修改后的makefile,complete:power echo”Build complete” power:$(OBJECTS) $(CC)$(OPTIONS)-O $ $ -lm echo”The executable is in th

15、e power file” main.o:main.h input.h compute.h compute.o:compute.h input.o:input.h power.tar:makefile $(HEADERS) $(SOURCES) tar -cvf $ $ clean: rm-f *.o core power,虚目标,2-6虚目标,不存在的文件,而且也无需创建他们 允许你强制执行某些事件,而这些事件在正常规则中是不会发生的 规则虚目标和先决条件 如果虚目标作为先决条件使用,它必须作为目标出现在某处 虚目标总是使与之有关的命令被执行 虚目标作为先决条件,总是是相应的目标重建,2-6

16、虚目标,常见虚目标列表,2-6虚目标,$cat makefile INSTALLDIR=/home/sarwar/courses/bin install:client server cp f $ $(INSTALLDIR) rm f *.o $ cd $(INSTALLDIR);chmod 755 $ uninstall: cd $(INSTALLDIR);rm client server client:client.o miscc.o rcopyc.o gcc client.o miscc.o rcopyc.o lnsl -o client client.o:client.c netc.h

17、rcopy.h gcc -c clientc $make install . . . . . . .,1.不存在的文件,仅为完成某些功能 2.如果目录下存在这个同名文件会出现什么情况?,2-7特殊目标,上述问题用.PHONY的特殊目标解决 .PHONY:clean clean: rm rf *.o,2-7特殊目标,2-8一般性语法错误,Tab键 在和换行符插入了空格,$make Makefile:4:*missing separator.Stop $cat t Makefile $grep Makefile,$ cat e Makefile $grep $ Makefile,2-9命令行的使用

18、和调试,使用非标准的Makefile名称 从标准输入读取 显示所执行的顺序,$ make f prog1.makefile,$make -f-,$make -n,3-1autoconf,创建安装shell脚本的工具 configure 一旦configure生成,无需autoconf Autoconf打包的软件 ./configure make make install,3-1autoconf,Autoconf 实际上是个工具集,3-1autoconf,确定条件编译 $ifnames *.c *.h 输出为条件定义的宏的列表,以及定义他们的文件 创建configure.in文件 $autosc

19、an $move configure.scan configure.in,3-1autoconf,编辑configure.in文件 由m4宏指示字组成 被autoconf解析,生成configure脚本 创建makefile.in文件 修改自己的makefile文件来包含autoconf产生的定义,任务的主要部分,3-1autoconf,创建config.h.in文件 $autoheader config.h文件的输入 更新源文件 所有的考虑移植的源文件,需要包含config.h,3-1autoconf,创建安装脚本 $autconf 复制autoconf脚本 Autoconf 的其他脚本 c

20、onfig.guess Config.sub Install-sh 包含在目录/usr/lib/autoconf中,阶段总结,Makefile 简单变量的定义 Makefile的内置变量 虚目标与常用虚目标 Makefile常见的语法错误 Autoconf 打包软件的安装 用autoconf打包软件,本章总结,简介,多模块软件的编译和链接,Makefile和Make Rules,默认规则,虚目标,特殊目标,一般性语法错误及其纠正措施,autoconf,多模块软件、依赖树和make,Make实用程序,make实用程序对简单变量的支持,内建变量,命令行的使用和调试,多模块软件编译的困境,分析mak

21、e的原理,make程序的用法,以及Makefile的语法,规则,以及makefile对变量的 支持,描述了虚目标,以及makefile的语法错误检查方法,描述了autoconf打包软 件的安装,以及如何 用autoconf打包软件,实验,任务1:为hello world 编写makefile 任务2:autoconf的简单应用,任务1,注意事项 提示1: 编写一个头文件,两个C文件 main.c 是主程序 hello.c编写打印”hello,world”的函数 提示2: 编写makefile,建立直接的依赖关系 使用默认规则 定义变量CROSS_COMPILE := CC := (CROSS_COMPILE)gcc,任务2,确认系统中存在autoscan,autoconf命令 编写hello.c以及makefile,44,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1