1. 程式人生 > >Linux入門級gdb除錯--C/C++語言

Linux入門級gdb除錯--C/C++語言

Linux上面一般使用gdb來進行程式碼的除錯,除錯我目前知道的方法是:首先將寫好的LinuxC/C++程式碼編譯成可執行檔案,注意編譯的時候生成目標檔案.o的時候必須加上-g引數,-g引數是表示生成的.o檔案是包含有列印資訊的,如果不加的話,無法進行除錯,因為沒有任何列印資訊。下面我將根據一個簡單的demo程式來進行講解gdb的除錯:

首先我的檔案目錄如下:

.
├── add.c
├── include.h
├── main.c
└── Makefile

0 directories, 4 files

add.c檔案內容:

int add(int a,int b)
{
    return (a+b);
}

include.h檔案內容:

/*extern function*/
extern int add(int a,int b);

main.c內容如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "include.h"

int main(void)
{
    printf("I need a pause here.\n");
    printf("And here too.\n");
    sleep(1);

    int a = 8;
    int b = 9;
    int c = 0;
    c = add(a,b);

    printf("add a+b = %d\n",c);

    printf("Test OK!\n");

    return 0;
}

Makefile內容如下(此處使用makfile是為了方便編譯成可執行檔案,你也可以自己手動在命令列進行編譯)

#makefile demo
OBJ_FILE=main.o add.o
CC=gcc

main:$(OBJ_FILE)
    $(CC) $^ -o [email protected]
add.o:add.c
    $(CC) -g -c add.c
main.o:main.c
    $(CC) -g -c main.c


.PHONY:clean
clean:
    @echo "clean project"
    -rm *.o
    @echo "clean complete"
~                                                                               
~    

注意編譯成add.o和main.o的時候必須加上引數-g,否則無法除錯

然後執行命令#make,在目錄下得到可執行檔案main

.
├── add.c
├── add.o
├── include.h
├── main
├── main.c
├── main.o
└── Makefile

0 directories, 7 files

最後是進行gdb的除錯,當前資料夾內執行gdb,然後進入到除錯介面,如下所示:

[email protected]:~/jelly/MakeTest/makeadd$ gdb
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) 

然後輸入命令

(gdb) file main
Reading symbols from main...done.
(gdb)

此命令表示載入可執行檔案main檔案

然後輸入l檢視當前可執行檔案的原始碼內容

(gdb) 
(gdb) file main
Reading symbols from main...done.
(gdb) l
1	#include <stdio.h>
2	#include <stdlib.h>
3	#include <unistd.h>
4	#include "include.h"
5	
6	int main(void)
7	{
8		printf("I need a pause here.\n");
9		printf("And here too.\n");
10		sleep(1);
(gdb) 

按回車鍵可以繼續檢視後面的原始檔內容

找到需要打斷點的地方的函式,比如在main函式處打斷點,輸入b main

6	int main(void)
7	{
8		printf("I need a pause here.\n");
9		printf("And here too.\n");
10		sleep(1);
(gdb) 
11		
12		int a = 8;
13		int b = 9;
14		int c = 0;
15		c = add(a,b);
16	
17		printf("add a+b = %d\n",c);
18		
19		printf("Test OK!\n");
20	
(gdb) b main
Breakpoint 1 at 0x4005be: file main.c, line 8.
(gdb) 

然後輸入命令r執行程式,此時程式就會停留在剛剛打斷點的函式處

(gdb) b main
Breakpoint 1 at 0x4005be: file main.c, line 8.
(gdb) r
Starting program: /home/zqq/jelly/MakeTest/makeadd/main 

Breakpoint 1, main () at main.c:8
8		printf("I need a pause here.\n");
(gdb) 

輸入n是進行單步除錯,如果遇到函式,則輸入s進入函式內部進行除錯

如果想看那個變數的值則使用print命令,如下所示:

(gdb) n
main () at main.c:17
17		printf("add a+b = %d\n",c);
(gdb) n
add a+b = 17
19		printf("Test OK!\n");
(gdb) print c
$1 = 17
(gdb) 

上面原始碼可以看到列印c,此時c的值是17

如果你不需要再進行除錯了就輸入c然後回車,全部執行,最後結束。

(gdb) print c
$1 = 17
(gdb) c
Continuing.
Test OK!
[Inferior 1 (process 4193) exited normally]
(gdb) 

以上就是Linux下面gdb的簡單除錯C/C++程式碼的方法!!!相互學習學習!!!