较详细的gdb入门教程 您所在的位置:网站首页 如何使用gdb进行断点调试设置 较详细的gdb入门教程

较详细的gdb入门教程

2023-07-07 18:45| 来源: 网络整理| 查看: 265

前言

本文选自 较详细的gdb入门教程 - Zesty_Fox。

本篇教程适用于 Windows,macOS 及 Linux,但由于 Windows 的自带终端很难用,所以体验可能不太好。Windows 10 建议安装 Windows Terminal 以取得最佳体验。

你是否为 C/C++ 下的调试而苦恼?你是否苦于 Dev-C++ 调试烦人的问题(如调不了 STL、结构体数组要一层一层展开)?那么,gdb 很可能是你的最佳选择。

gdb 是一个命令行下的、功能强大的调试器。看到命令行下,是不是有点害怕?没关系,本文最后会介绍一些图形前端,但建议先学习一些基础命令。

示例代码:(example.cpp,以下调试命令均以此代码为准)

博主太懒了,只写了个求阶乘

#include #include using namespace std; int f(int x){ int ans=1; for(int i=1;i g++ example.cpp -o example -g [编译,无提示] > gdb ./example

然后,你可能会见到如下的界面:

GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./example...done. (gdb)

这样,你就进入 gdb 的命令行环境里了。

其中,第一行是版本信息,倒数第二行表示正载入符号表,最后一行 (gdb) 则是** gdb 的提示符**。

请注意,若你倒数第二行有 (no debugging symbols found) 字样,请确保在编译选项里加上 -g 选项。

当然,如果你直接输入 gdb 启动,不加文件名,也可以。只是,你要使用 file 命令手动载入可执行文件。

以后出现的所有命令,都是在 gdb 的环境,而非系统 shell 的环境执行的。

命令:file(简写fil) 格式:file 可执行文件名 作用:载入当前目录下的对应名称的可执行文件。

例子:

(gdb) file example Load new symbol table from "example"? (y or n) y Reading symbols from example...done.

命令:list(简写为l) 格式:list [行号] 作用:打印给定行号周围 1010 行的源代码。若不提供行号,则接续打印上次的源代码。

这里提到了一个简写的概念。什么是简写呢?简写是为了简化命令的。比如,打一个 list 还是有些麻烦的。这时,我们可以输入它的简写 l 。你可以认为一个命令与它的简写是完全等价的。以后若提到简写,不再解释。

例子:

(gdb) l 7 2 #include 3 using namespace std; 4 int f(int x){ 5 int ans=1; 6 for(int i=1;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有