Files
CPlusPlusThings/site_extras/cpp-tools-gdb-debugging-guide.md
light-city dfe1c933b3 docs: address community issue feedback (#5 #338 #142 #246 #136 #303)
- const README: rewrite the space-saving claim per issue #5 follow-ups
  (constant-expression const vs #define immediates; which const qualify
  as constant expressions)
- const README: fix the class-section diagnosis per issue #338 (overload
  resolution picks the const overload; the restriction only surfaces
  once that overload is removed)
- sizeof README: virtual-inheritance result is compiler-dependent
  (GCC merges vbptr+vfptr -> 16, MSVC keeps them separate -> 32) #246
- static README: new section on internal linkage / access restriction
  with anonymous-namespace comparison #142
- 10-day practice: full problem list table + fixed day links #136
- new design_pattern/singleton/singleton_default.hpp: reusable
  thread-safe CRTP singleton template, compile+concurrency tested #303
2026-10-13 19:23:54 +08:00

34 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
> 来自 [C++那些事网站](https://light-city.github.io/stories_things/cpp-tools/gdb-debugging-guide/) 的文章,由站点同步脚本导出。
# GDB 调试 C++ 程序
当使用GNU Debugger(GDB)调试C代码时,你会发现它是UNIX系统上最受欢迎的调试器,用于调试C和C++程序。下面是一个简明易懂的使用GDB调试C代码的解释:
首先,在命令行中切换到包含代码的当前文件夹,并运行以下命令:
gcc -g -Wall code.c -o output
其中,`-g`选项对于使用GDB工具是必要的。`-Wall`选项用于显示警告信息。这将创建一个名为"output"的可执行文件。
然后,运行以下命令打开GDB对话框并从output文件中获取符号信息(或简单地说,设置整个环境以调试code.c):
gdb output
使用以下命令可以帮助你进行调试:
- `list`:列出源代码。
- `b x`:在x处设置断点。x可以是模块的名称或行号。例如,x可以是main等函数名称。
- `r`:执行程序(直到断点),并提供结果。即,在断点处运行代码并生成结果。[如果有参数,则为r 参数1 参数2 参数3 ...]
- `info breakpoints x`:给出断点x的详细信息。
- `info breakpoints`:给出所有断点的详细信息。
- `delete x`:删除断点x。
- `disable x`和`enable x`:禁用和启用断点x。
- `print p`:打印变量p的值。
- `display p`:在每次运行后显示变量p的值(自动化整个过程)。
- `c`:继续执行程序(跳过for循环等迭代)。使用`c-iter1`跳过第一次迭代,再次输入`c`跳过第二次迭代,依此类推。
- 你还可以使用额外的条件,例如`b x if (y > m)`,意思是如果y的值大于m,则在第x行处设置断点。
- GDB还可以在多个文件中设置断点。例如,如果program.c中调用了function.c中的一个函数,你可以使用`b function.c:x`在function.c的第x行设置断点。
- `bt`:用于回溯代码执行的路径。`bt`会给出一个索引列表,显示代码经过的进程。例如:#0 进程0的描述,#1 进程1的描述,依此类推。其中,0是最近的进程。
- 输入`f x`将跳转到进程x(`f 0`将跳转到进程0),并给出对应函数执行时的变量值。
- `s`:用于进入函数并获取更多详细信息。
- `next`:用于逐行执行代码。