GDB的使用方法簡述

2021-09-10 09:21:32 字數 1100 閱讀 7602

乙個除錯示例

——————

源程式:tst.c

1 #include 2

3 int func(int n)

4 {5 int sum=0,i;

6 for(i=0; i編譯生成執行檔案:(linux下)

hchen/test> cc -g tst.c -o tst

使用gdb除錯:

hchen/test> gdb tst <---------- 啟動gdb

gnu gdb 5.1.1

gdb is free software, covered by the gnu general public license, and you are

welcome to change it and/or distribute copies of it under certain conditions.

type "show copying" to see the conditions.

there is absolutely no warranty for gdb. type "show warranty" for details.

this gdb was configured as "i386-suse-linux"...

(gdb) l <-------------------- *l命令相當於list,從第一行開始例出原碼。*

1 #include 2

3 int func(int n)

4 {5 int sum=0,i;

6 for(i=0; i

好了,有了以上的感性認識,還是讓我們來系統地認識一下gdb吧。

使用gdb

————

一般來說gdb主要除錯的是c/c++的程式。要除錯c/c++的程式,首先在編譯時,我們必須要把除錯資訊加到可執行檔案

中。使用編譯器(cc/gcc/g++)的 -g 引數可以做到這一點。如:

> cc -g hello.c -o hello

> g++ -g hello.cpp -o hello

如果沒有-g,你將看不見程式的函式名、變數名,所代替的全是執行時的記憶體位址。

gdb使用方法

一 程式例子 test.c include int fun int n return sum int main printf d n sum int ret 0 ret fun sum printf d n ret return 0 二 編譯 g表示開啟除錯開關 sudo gcc g test.c ...

gdb使用方法

教程見 編譯程式時需要加上 g,之後才能用gdb進行除錯 gcc g main.c o main gdb中命令 gdb tui a.out 除錯中顯示源 gdb help 檢視命令幫助,具體命令查詢在gdb中輸入help 命令,簡寫h gdb start 單步執行,執行程式,停在第一執行語句 gdb...

gdb使用方法簡介

編譯時必須加上引數 g 例 g g temp.cpp o temp.通過gcc編譯生成可執行檔案才能用gdb進行除錯。進入gdb介面 gdb temp.提示符變成 gdb 1 檢視檔案 在gdb中鍵入 l list 就可以檢視所載入的檔案 2 設定斷點 只需在 b 後加入對應的行號即可 這是最常用的...