標籤:
linux-c/c++調試利器gdb、ddd小試
原文連結: http://deepfuture.iteye.com/blog/749148
部落格分類:
CC++C#LinuxUbuntu
一、gdb,在shell視窗調試
main.c內容:
main.c
#include <stdio.h>
int main()
{
int y=0;
for (int i=0;i<10;i++){
y+=i;
}
return 0;
}
深未來技術原創文章,如轉載,請註明來源http://deepfuture.iteye.com/
編譯:
[email protected]:~/test$ gcc -ggdb -std=c99 -o main main.c
啟動gdb
1、gdb 執行檔案名稱
[email protected]:~/test$ gdb main
GNU gdb (GDB) 7.1-ubuntu
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 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 "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/deepfuture/test/main...done.
2、list為列出原始碼
(gdb) list
1
2 #include <stdio.h>
3 int main()
4 {
5 int y=0;
6 for (int i=0;i<10;i++){
7 y+=i;
8 }
9 return 0;
10 }
3、運行(gdb) runStarting program: /home/deepfuture/test/main Program exited normally.4、退出gdb(gdb) quit5、啟動gdb後,再設定要載入的檔案(gdb) file mainReading symbols from /home/deepfuture/test/main...done. 6、設定斷點,,使用break行號 (gdb) listwarning: Source file is more recent than executable.12 #include <stdio.h>3 int main() 4 {5 int y=0;6 for (int i=0;i<10;i++){7 y+=i;8 }9 return 0;10 } (gdb) break 7Breakpoint 1 at 0x80483ca: file main.c, line 7. 7、運行,run(gdb) runStarting program: /home/deepfuture/test/main Breakpoint 1, main () at main.c:77 y+=i;8、離開這個斷點,繼續運行(gdb) cContinuing.9、監視變數(gdb) watch yHardware watchpoint 2: y10、檢查變數變化(gdb) cContinuing.Hardware watchpoint 2: y Old value = 1New value = 3main () at main.c:66 for (int i=0;i<10;i++){(gdb) cContinuing. Breakpoint 1, main () at main.c:77 y+=i;(gdb) cContinuing.Hardware watchpoint 2: y Old value = 3New value = 6main () at main.c:66 for (int i=0;i<10;i++){ 二、ddd,使用可以在選擇行或某變數後,watch,break等按鈕,很方便右中部的面板是一些流程調試
圖形方式調試
linux-c/c++調試利器gdb、ddd小試