1、编译源代码
C:MinGW\bin>gcc.exe -g -o program.exe program.c
编译选项上要加上“g”,这样生成的目标程序会含有调试内容,再用gdb调试的时候才能使用。显然加上“g”选项生成的应用程序会比不加的大,但两者运行时没有差别。
2、启动调试
C:MinGW\bin>gdb.exe program.exe
3、设置断点并启动运行
(gdb)break main
(gdb)start
不能直接start,因为程序运行太快了,直接start就运行到程序停止的地方。
break main在start命令之前,设置程序运行的断点,这样start后程序就运行到main处中断。也可以用命令“break FILENAME:LINENO",程序会在FILENAME指定的文件的LINENO指定行停下,例如"break mycode.cpp:4”。
4、其他命令
print VARNAME
. That's how you print values of variables, whether local, static, or global. For example, at thefor
loop, you can typeprint temp
to print out the value of thetemp
variable.step
This is equivalent to "step into".next
oradv +1
Advance to the next line (like "step over"). You can also advance to a specific line of a specific file with, for example,adv mycode.cpp:8
.bt
Print a backtrace. This is a stack trace, essentially.continue
Exactly like a "continue" operation of a visual debugger. It causes the program execution to continue until the next break point or the program exits.