Linux入门级gdb调试--C/C++语言

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013896064/article/details/83147698

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 $@
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,然后进入到调试界面,如下所示:

zqq@zqq-VirtualBox:~/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++代码的方法!!!相互学习学习!!!

猜你喜欢

转载自blog.csdn.net/u013896064/article/details/83147698