Code::Blocks Debug Shortcut and Example

作为一个优秀的IDE,Code::Blocks自然也提供Debugger的功能,以下我们介绍如何使用Code::Blocks的Debugger和使用实例。下面是CodeBlocks Debug的快捷键定义():

Debug - F8 ,Continue - Ctrl + F7 ,Step over a code block  - F7

Step into a code block - Shift + F7,Step out of a code block - Ctrl + Shift + F7

Toggle breakpoint - F5Run to cursor - F4

Previous error - Alt + F1Next error - Alt + F2

这篇文章来自于一位我们的台湾朋友,我把其中的一些术语做了修改以便于大家阅读。原文链接已附上可参阅。使用的代码范例如下

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])  
  4. {  
  5. int i;  
  6. for(i = 0 ; i < 10 ; ++i){  
  7.         printf("Hello Baby\n");  
  8.     }  
  9.     system("PAUSE");     
  10. return 0;  
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int i;
    for(i = 0 ; i < 10 ; ++i){
        printf("Hello Baby\n");
    }
    system("PAUSE");
    return 0;
}

设定断点

設定中斷點的方式和大部份IDE差不多,如下圖所示,點擊行數數字旁的空白,或是按右鍵選擇"Add breakpoint",就可以新增中斷點

Add breakpoint

如果正確新增一個中斷點,會在該行數字旁邊看見一個小紅點

開始除錯

當新增完中斷點以後就可以開始除錯,在除錯之前請先注意,請注意你的Build target應該要是Debug,如果設為Release就無法除錯,請改回成Debug,或是編譯的參數裡要有 -g,要設定編譯參數,由選單的Project -> Build options,開啟以後的Compiler setting頁面就可以看到Produce symbols [-g],打勾以後應該就可以進行除錯

除錯可以由選單的 Debug -> Start ,或是按F8開始進行除錯

Code::Blocks - start debugger

如果沒有錯的話,應該可以看到程式執行,並且有個黃色的箭頭停在小紅點,這表示已經中斷了,等其它的指令

Code::Blocks 中斷點 目前行執行哪行

除錯工具列

接下來我們介紹如何進行步進和除錯,以下是除錯用的工具列

Code::Blocks 除錯功能工具列

在此介紹每個按鈕的功能

  1. 開始除錯,也就是我們之前開始除錯所執行的功能
  2. 執行程式,並且在游標位置中斷,當你不想設中斷點,可是卻又想在某點中斷時,可以移動游標到你想中斷的那行程式碼上,再使用此功能
  3. 下一行,如它圖示所暗示的,它並不會進入呼叫,而是直接跨過去,這是最常用的功能
  4. 下一個指令,這是當你想看組合語言的下一個指令時所用的功能
  5. 進入,此功能會跳入函數呼叫,如果你對函數裡的程式有興趣,就使用此功能
  6. 離開,當你想跳出此函數呼叫到上一層時使用此功能
  7. 中止除錯,如果你找到錯誤了,或是不想除錯了,就可使用此功能,有時直接關閉程式並不會終止除錯,也可以使用此功能
  8. 除錯相關視窗,當你想查看CPU的暫存器內容,或是查看某個變數值時,可以開啟相關的視窗
  9. 資訊視窗,開啟一些比較瑣碎的資訊視窗

观察变量

想要觀察變數的值,可以按右邊數過來第二個視窗圖案的按鈕,然後選Watches,應該就可以看見跑出一個Watch視窗來

Code::Blocks 查看變數

想要增加觀察某個變數,可以按右鍵,Add watch

Code::Blocks 新增察看變數

其它功能

其它功能,例如观察呼叫堆栈、CPU寄存器等等,都在打开观察变量窗口的同样按钮中,在此就不多加赘述

http://ez2learn.com/index.php/c-tutorials/codeblocks-tutorials/206-codeblocks-debugger

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Related posts:

  1. AVL树Source Code
  2. Setting up Code::Blocks to work with SDL - 多媒体编程库
  3. Debugging with Code::Blocks
  4. QT4+MinGW+Code::Blocks IDE集成
  5. Code::Blocks IDE: 手动安装和配置编译器指南
  6. CodeBlocks下SDL编译成功实例
  7. Code::Blocks IDE in openSUSE 11.1编译和安装指南
  8. 加法?减法:CodeLite or Code::Blocks just for another sourceinsight
  9. ProjectManager, Project and ProjectFile in Code::Blocks
  10. C/C++ IDE Code::Blocks安装过程(含中文包下载)

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu