GDB c++ 调试
GDB: GNU Debugger
GNU: GNU is Not Unix
UNIX/LINUX操作系统下的, 基于命令行的, 功能强大的程序调试工具.
程序编译
首先,在debug之前要对程序进行编译,可以用 gcc 进行编译。
检查是否安装 gcc:
rpm -qa | grep gcc
若没有gcc, 请先安装。
1. 安装gcc (CentOS):
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
yum -y install wget httpd-tools vim
root@DTSEACLI409540:/ # gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
2. 编译
由于我的代码环境是 c++ 11, 所以用以下命令可以编译:
g++ -g -std=c++11 skip_list.cpp -o skip_list
若不是 c++ 11, 则不需要参数 “-std=c++11”。
GDB debug
1. 安装 gdb
检查 GDB 是否安装:
rpm -qa | grep gdb
如果你使用的是 centos 的话, 一般是自带 GDB 的安装包的。可以直接用 yum 安装。
yum -y install <>
2. 进入调试环境
gdb <executable c++ file>
3. 基本操作
(gdb) run //运行程序
(gdb) list <number> //列出该行代码前后十行
(gdb) list <function> //列出该函数前后十行
(gdb) list //不加参数,则打印上次list的后面十行
(gdb) quit //退出
例如:
(gdb) list 2
1 #include <iostream>
2 #include <vector>
3 #include <numeric>
4 #include <random>
5 #include <time.h>
6
7 using namespace std;
8
9 /*The structure of node in skiplist,
10 includes key, pointer and constructor.
(gdb) list main
290 }
291 }
292
293 //main function
294 int main()
295 {
296 SkipList l;
297 for (size_t i = 0; i < 50; i++)
298 {