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.