文档详情

嵌入式LINUXC程序设计剖析.ppt

发布:2016-11-27约4.88千字共31页下载文档
文本预览下载声明
第7讲 LINUX-C程序设计 GNU C简介 GNU C的扩展 LINUX函数库 Makefile文件简介 1 Linux GNU-C简介 GNU简介 开发工具软件gcc的安装 1.1 GNU简介 GNU是GNUs?Not?Unix的缩写 是由自由软件基金会?(Free?Software?Foundation,简称FSF)的董事长Richard?M.?Stallman?(RMS)于1984年发起的。?? GNU简介 在GNU Manifesto(GNU宣言)中对GNU的精神进行了阐述:软件的源代码应该自由流通,软件开发者应该做的不是把源代码据为己有,赚取发行可执行文件的金钱,而是应该赚取整合与服务的费用。因为源代码自由流通的软件才能让软件的质量提高,让软件开发人员可以自由的与他人交换心得,不受知识产权的约束。 GNU简介 GNU开发工具 自由软件 完备的工具链: GCC、binutils、gdb、 GNU make、patch、CVS 、开发库 命令行方式:使用稍复杂、功能强大 下载: 1.2 gcc的安装 将博创兴业科技有限公司提供的附带开发工具光盘插入 CDROM,然后执行以下命令: mount /dev/cdrom /mnt gcc的安装 1.3 C的可移植性考虑 避免使用魔数(Magic Number) 通过宏定义引用魔数: #define intsize 4 #define BUFSIZE 256 #define rSYSCFG (*(volatile unsigned *)0x1c00000) C的可移植性考虑 程序分层 例如 TCP/IP uCOS2 数据类型长度 对齐问题 大小端问题 枚举类型 减少内嵌汇编 2 GNU C扩展 64位整型数据类型 内联函数 attribute关键字 单行注释 64位整型数据类型 内联函数 attribute关键字 关键字attribute通过向GCC指明有关代码的更多信息来帮助代码优化工作进行的更好。 attribute关键字(续) int main( ) { float t __attribute__ ((unused)); return 0; } 例3-12 #include stdlib.h void quit() __attribute__ ((noreturn)); void quit() { exit(1); } int test(int n) { if ( n 0 ) { quit(); } else return 0; } 【例3-13】 /* ch3_13.c */ #include stdlib.h struct s { int a[2] __attribute__ ((aligned (8))); }; struct t { char a; int b[2] __attribute__ ((packed)); }; 单行注释 int main() { //long long t1; int t4=3; return 0; } switch case 简写 /* ch3_14.c */ #include stdio.h int main() { int t=2; /*传统方式*/ switch(t) { case 0 : case 1 : case 2 : printf(012\n); break; case 3 : case 4 : case 5 : printf(345\n); } switch case 简写 /*GCC扩展方式*/ switch(t) { case 0 ... 2 : printf(012\n); break; case 3 ... 5 : printf(345\n); } return 0; } 预定义宏 /* ch3_15.c */ #include stdio.h void f(void) { printf(This is function %s \n, __FUNCTION__); } int main( ) { printf(This is function %s \n, __FUNCTION__); f(); return 0; } 标准C中的预定义宏 C89规定的预定义宏 __LINE__ 代表当前行号 __FILE__ 代表当前文件名 __DATE__ 代表当前日期 __TIME__ 代表当前时间 __STDC__ 代表是否为标准C编译器 结构体初始化 struct file_operations ext2_file_operations = {
显示全部
相似文档