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.i
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$ls
hello.c hello.i
hello.i------à
…………………………
extern int pclose (FILE *__stream);
extern char *ctermid (char *__s) __attribute__((__nothrow__));
# 906 "/usr/include/stdio.h" 3 4
extern 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.S
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$ls
hello.c hello.i hello.S
hello.S----à
.file "hello.c"
.section .rodata
.LC0:
.string "Hello!"
.text
.globl main
.type main, @function
main:
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.o
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$ls
hello.c hello.i hello.o hello.S
4、链接,生成可执行文件hello
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$ls
hello hello.c hello.i hello.o hello.S
5、运行可执行文件,输出Hello!
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.2$./hello
Hello!
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 add
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.3$ls
add add.c
3、进入gdb调试
eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.3$gdb add
GNU gdb (Ubuntu/Linaro 7.2-1ubuntu11) 7.2
Copyright (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 15
Breakpoint 1 at 0x80483eb: file add.c, line 15.
6、查看断点信息
(gdb) info b
Num Type Disp EnbAddress What
1 breakpoint keep y 0x080483eb in main at add.c:15
7、运行代码
(gdb) r
Starting program:/home/eastmoon/work/guoqian/1/1.3/add
Breakpoint 1, main () at add.c:15
15 a =add(a, b);
8、查看变量值
(gdb) p a
$1 = 4
(gdb) p b
$2 = 5
9、单步运行,可进入函数
(gdb) s
add (x=4, y=5) at add.c:5
5 return x + y;
(gdb) s
6 }
(gdb) s
main () at add.c:17
17 printf("a+b = %d\n", a);
(gdb) p a
$3 = 9
(gdb) p b
$4 = 5
10、单步运行,不可进入函数
(gdb) n
a+b = 9
19 return 0;
11、运行程序,直到函数结束
(gdb) finish
"finish" not meaningful in the outermostframe.
12、恢复程序运行
(gdb) c
Continuing.
Program exited normally.
13、退出gdb调试
(gdb) q