cc -g -o compute compute.o add.o subtract.o multiply.o divide.o
compute.o: compute.c
cc -c -g compute.c
add.o: add.c add.h
cc -c -g add.c
subtract.o: subtract.c subtract.h
cc -c -g subtract.c
multiply.o: multiply.c multiply.h
cc -c -g multiply.c
divide.o: divide.c divide.h
cc -c -g divide.c
clean:
rm compute.o add.o subtract.o multiply.o divide.o
将这几个文件放在一个文件夹test下,在命令行界面键入make,则Makefile脚本被执行,如果不想加调试信息,则把脚本里的-g去掉。-g是为了编译时加入调试信息在可执行文件和目标文件里。 #cd test test#make ---运行Makefile脚本 cc -c -g compute.c cc -g -o compute compute.o add.o subtract.o multiply.o divide.o test#ls add.c compute divide.c Makefile multiply.o subtract.o add.h compute.c divide.h multiply.c subtract.c add.o compute.o divide.o multiply.h subtract.h test#./compute ---执行compute 具体的输入不写了,该运行gdb调试程序了 test#gdb compute ---以下为gdb调试的命令行,以(gdb)为开头的 GNU gdb 6.8 Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later < This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i686-pc-linux-gnu"... (gdb) l ---list源代码 5 extern substract(int,int); 6 extern multiply(int,int); 7 extern divide(int,int); 8 9 int main() 10 { 11 int select; 12 int a,b; 13 int result; 14 loop1: printf("please input select value(1:+,2:-,3:*,4:/):"); (gdb) l ---继续list 15 scanf("%d",&select); 16 if(select<=0||select>=5)goto loop1; 17 18 printf("\nplease input a b value:"); 19 scanf("%d%d",&a,&b); 20 switch(select) 21 { 22 case 1:result=add(a,b);break; 23 case 2:result=subtract(a,b);break; 24 case 3:result=multiply(a,b);break; (gdb) break 16 ---设置断点在第16行 Breakpoint 1 at 0x8048410: file compute.c, line 16. (gdb) break add ---设置断点在add函数名入口 Breakpoint 2 at 0x80484df: file add.c, line 6. (gdb) r ---运行run Starting program: /root/Desktop/test/compute please input select value(1:+,2:-,3:*,4:/):3
Breakpoint 1, main () at compute.c:16 16 if(select<=0||select>=5)goto loop1; (gdb) c ---继续运行continue Continuing.
please input a b value:3 5 the result is: 15 Program exited normally.
以下为网上抄录: 更多的命令: 调试程序使用的键
r run 运行.程序还没有运行前使用
c cuntinue 继续运行。运行中断后继续运行
q 退出
kill 终止调试的程序
h help 帮助
; 命令补全功能
step 跟入函数
next 不跟入函数
b breakpoint 设置断点。
用法:
b 函数名 对此函数进行中断
b 文件名:行号 对此文件中指定行中断.如果是当前文件,那么文件名与:号可以
省略
看当前断点数使用info break.禁止断点disable 断点号.删除delete 断点号.
l list 列出代码行。一次列10 行。连接使用list将会滚动显示. 也可以在list 后面
跟上 文件名:行号
watch 观察一个变量的值。每次中断时都会显示这个变量的值
p print 打印一个变量的值。与watch不同的是print只显示一次
这里在顺便说说如何改变一个 value. 当你下指令 p 的时候,例如你用 p b,
这时候你会看到 b 的 value, 也就是上面的 $1 = 15.
你也同样可以用 p 来改变一个 value, 例如下指令 p b = 100 试试看,
这时候你会发现, b 的 value 就变成 100 了:$1 = 100.