windows下使用MinGW的調試工具gdb.exe調試C程序


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 the for loop, you can type print temp to print out the value of the temp variable.
  • step This is equivalent to "step into".
  • next or adv +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.

 

參考資料:http://stackoverflow.com/questions/4671900/how-do-i-use-the-mingw-gdb-debugger-to-debug-a-c-program-in-windows


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM