C语言gcc强化训练2

上传人:xmg****18 文档编号:119872747 上传时间:2020-01-28 格式:PPT 页数:82 大小:1,014.50KB
返回 下载 相关 举报
C语言gcc强化训练2_第1页
第1页 / 共82页
C语言gcc强化训练2_第2页
第2页 / 共82页
C语言gcc强化训练2_第3页
第3页 / 共82页
C语言gcc强化训练2_第4页
第4页 / 共82页
C语言gcc强化训练2_第5页
第5页 / 共82页
点击查看更多>>
资源描述

《C语言gcc强化训练2》由会员分享,可在线阅读,更多相关《C语言gcc强化训练2(82页珍藏版)》请在金锄头文库上搜索。

1、www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 嵌入式Linux GCC培训 主讲老师 欧阳坚 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 GCC是什么 GCC GNU Compiler Collection GCC支持多种硬件平台和操作系统 能编译多种语言 C C Java Ada95 Objective C etc GCC与G 的关系 GCC用于编译多种语言编写的程序 主要是C G 用于编译C 程序 以GCC为基础 编译过程 中加入了C 的支持库 参数

2、与GCC基本一致 可以利用GCC编译C 程序 但是需要在参数中加 入引用的C 库 比如libstdc 如gcc o out lstdc main cc www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 可执行程序的生成过程 预处理 Preprocessing 分析各种预处理命令 如 define include if等 编译 Compilation 根据输入文件产生汇编语言 的程序 汇编 Assembly 将汇编语言输入 产生扩展 名为 o的目标文件 链接 Linking 以 o目标文件 库文件作为输入 生成可执行文件 源程序文

3、件 h c cc etc 经预处理的文件 i ii 汇编语言文件 s 目标文件 o 可执行程序 out www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 GCC支持的文件类型 C文件 cC源代码 hC头文件 C 文件 file hh file hC 头文件 file C file cc file cxx等C 源文件 预处理后的文件 file i预处理后的C源文件 file ii预处理后的C 源文件 编译后的文件 o目标代码 obj s汇编代码文件 file a目标文件库 www embedtrain org www mobile

4、train org 千锋3G嵌入式移动互联网技术研发中心 GCC编译选项 命令行 gcc options infile E不生成文件 只输出预处理结果 输出终端 S只预处理和编译 把文件编译成为汇编代码 greet s c预处理 编译和汇编 生成 o的obj文件 greet o o file输出名为file的可执行文件名 缺省为a out O O2优化编译 g 产生可用于调试的输出 Wall提示更多警告信息 Wstrict prototypes如果函数的声明或定义没有指出 参 数类型 编译器就发出警告 Wl option 将option作为选项传递给linker option 逗号分 割 如

5、Wl soname libmymath so 1 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 与库和路径相关选项 I dir 在dir这个目录寻找被include的文件 L dir 在dir这个目录寻找被 l的库 l name 链接库文件名为libname a 或libname so的库 fpic或 fPIC 产生位置无关的目标代码 以构造共 享库 shared library static 禁止与共享库链接 若没有 则优先 选择共享库链接 shared 产生共享库 在创建共享函数库时使用 www embedtrain o

6、rg www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 示例 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 与宏相关的选项 Dmacro 相当于在源程序中使用 define macro 1 Dmacro value Umacro 取消宏的定义 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 3 GCC编译过程 www

7、 embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 3 1 GCC编译过程 典型的编译过程 test c 预处理 test i 编译 test s 汇编 test o 连接 test cat test c 查看程序源代码 include int main int argc char argv printf hello world n return 0 gcc o test test c 编译连接程序 test 执行test程序 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发

8、中心 3 2 预处理 预编译命令 gcc o test i E test c 或者 cpp o test i test c 这里cpp不是值c plus plus 而是the C Preprocessor 执行结果 生成预处理后的文件test i 该文件包含 了test c需要的所有的类型和函数申明 原理 读取c源程序 对伪指令和特殊符号进行处理 包括宏 条件编译 包含的头文件 以及一些 特殊符号 基本上是一个替换的过程 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 Hello c include int main void

9、 printf hello n 预处理命令 gcc E hello c gcc E hello c o hello i 注释这一行看看预处理的结果 注意 include的作用和用途 E表示做预处理 o 表示预处理的输出存于 hello i文件中 而不是屏幕上 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 define用法 include define AA 100 int main void AA BB printf hello n 预处理命令 gcc E hello c DBB hello gcc E hello c DB

10、B printf hello gcc E hello c DBB 等效于 DBB 1 注释这一行看看预处理的结果 D表示在命令行中传入宏定义 DBB 后面是一个宏的定义 可以加双引号 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 define带参数 include define AA a b a b int main void AA int a 1 BB printf hello n 预处理命令 gcc E hello c DBB hello gcc E hello c DBB printf hello 注释这一行看看预处理

11、的结果 D表示在命令行中传入宏定义 DBB 后面是一个宏的定义 可以加双引号 展开就成了 int a 1 AA宏带两个参数 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 ifdef if defined if defined ifndef elif defined elif defined else if elif endif E D AA 100 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 define带参数 include ifdef AA aa e

12、lif defined BB bb elif defined CC cc else other endif int main void printf hello n ifdef AA 等效于 if defined AA 表示当定义了宏AA 表示除此之外的情况 表示否则定义了宏CC的情况 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 gcc E hello c DAA 1 aa int main void printf hello n www embedtrain org www mobiletrain org 千锋3G嵌入式

13、移动互联网技术研发中心 gcc E hello c DBB 1 bb int main void printf hello n www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 gcc E hello c DCC 1 cc int main void printf hello n www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 gcc E hello c other int main void printf hello n www embedtrain org

14、www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 if使用 www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 define带参数 include if AA aa elif BB bb elif CC cc else other endif int main void printf hello n if AA 表示AA非零的情况 也就是AA除了0其它数字都为真 表示除此之外的情况 elif BB 表示BB非零的情况 elif表示否则如果 www embedtrain org www mobiletra

15、in org 千锋3G嵌入式移动互联网技术研发中心 gcc E hello c DAA 1 aa int main void printf hello n gcc E hello c DAA 0 other int main void printf hello n www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 gcc E hello c DBB 1 bb int main void printf hello n gcc E hello c DBB 0 other int main void printf hello n ww

16、w embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 gcc E hello c DCC 1 cc int main void printf hello n gcc E hello c DCC 0 other int main void printf hello n www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 gcc E hello c other int main void printf hello n www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 的用法 在函数式宏定义中 运算符用于创建字符串 运算符后面应 该跟一个形参 中间可以有空格或Tab 例如 define STR s s char p STR helloworld 结果变成 char p helloworld www embedtrain org www mobiletrain org 千锋3G嵌入式移动互联网技术研发中心 的

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号