和菜鸟一起深入学习国嵌实验之gcc分步编译&&gdb调试

来源:互联网 发布:龙广958网络音乐吧录音 编辑:程序博客网 时间:2024/05/16 15:15

gcc分步编译

 

1、编写简单例子

#include <stdio.h> int main(void){   printf("Hello!\n");    return 0;}

 

2、预处理生成hello.i

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$gcc -E hello.c -o hello.ieastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$lshello.c hello.i

 

hello.i------à

…………………………extern int pclose (FILE *__stream);  extern char *ctermid (char *__s) __attribute__((__nothrow__));# 906 "/usr/include/stdio.h" 3 4extern void flockfile (FILE *__stream)__attribute__ ((__nothrow__)); extern int ftrylockfile (FILE *__stream)__attribute__ ((__nothrow__)) ; extern void funlockfile (FILE *__stream)__attribute__ ((__nothrow__));# 936 "/usr/include/stdio.h" 3 4 # 2 "hello.c" 2 int main(void){   printf("Hello!\n");    return 0;}

 

2、生成汇编代码helloS

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$gcc -S hello.i -o hello.Seastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$lshello.c hello.i  hello.S

 

hello.S----à

 

       .file  "hello.c"       .section   .rodata.LC0:       .string     "Hello!"       .text.globl main       .type       main, @functionmain:       pushl       %ebp       movl       %esp, %ebp       andl $-16, %esp       subl $16, %esp       movl       $.LC0, (%esp)       call  puts       movl       $0, %eax       leave       ret       .size main, .-main       .ident      "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4)4.5.2"       .section   .note.GNU-stack,"",@progbits

 

3、编译汇编代码生成hello.o

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$gcc -c hello.c -o hello.oeastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$lshello.c hello.i  hello.o  hello.S

 

4、链接,生成可执行文件hello

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$lshello hello.c  hello.i  hello.o hello.S

 

5、运行可执行文件,输出Hello!

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$./helloHello! 

 

gdb调试

 

1、  测试代码编写

#include <stdio.h> int add(int x, int y){    return x+ y;} int main(void){    int a, b;       a = 4;    b = 5;       a =add(a, b);    printf("a+b = %d\n", a);     return 0;}

 

2、编译代码生成可执行文件add

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.3$gcc -g add.c -o addeastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.3$lsadd  add.c

 

3、进入gdb调试

eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.3$gdb addGNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2Copyright (C) 2010 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later<http://gnu.org/licenses/gpl.html>This is free software: you are free to change andredistribute it.There is NO WARRANTY, to the extent permitted bylaw.  Type "show copying"and "show warranty" for details.This GDB was configured as"i686-linux-gnu".For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>...Reading symbols from /home/eastmoon/work/guoqian/1/1.3/add...done.(gdb)

 

4、查看文件

(gdb) l    3   int add(int x, int y)    4   {       5       return x + y;       6   }       7         8   int main(void)    9   {       10      int a, b;       11                12      a = 4;        (gdb)           13      b = 5;       14                15      a = add(a, b);       16            17      printf("a+b =%d\n", a);       18             19      return 0;       20  }       21 

 

5、设置断点

(gdb) b 15Breakpoint 1 at 0x80483eb: file add.c, line 15. 

6、查看断点信息

(gdb) info bNum    Type           Disp EnbAddress    What1      breakpoint     keep y   0x080483eb in main at add.c:15 

7、运行代码

(gdb) rStarting program:/home/eastmoon/work/guoqian/1/1.3/add Breakpoint 1, main () at add.c:1515      a =add(a, b); 

8、查看变量值

(gdb) p a$1 = 4(gdb) p b$2 = 5

 

9、单步运行,可进入函数

(gdb) sadd (x=4, y=5) at add.c:55      return x + y;(gdb) s6   }(gdb) smain () at add.c:1717     printf("a+b = %d\n", a); (gdb) p a$3 = 9(gdb) p b$4 = 5

 

10、单步运行,不可进入函数

(gdb) na+b = 919     return 0;

 

11、运行程序,直到函数结束

(gdb) finish"finish" not meaningful in the outermostframe. 

12、恢复程序运行

(gdb) cContinuing. Program exited normally. 

13、退出gdb调试

(gdb) q

 

原创粉丝点击