学 OI 需要知道的 GDB 技巧
在写了,别着急
水谷群的时候发现很多人不会动态调试,我很惊讶。对于像我这种自带调试功能重度沉迷用户,调题的时候不用动态调试非常罕见。
尽管如此,在 ccf 举办的、使用 noilinux 的比赛中,不给联网配置 vscode。自带的动态调试没了(悲)。好在还有 vscode 的壳下,负责真正功能的程序——GDB。
GDB 是 GNU 计划下,设计的动态调试工具。对于 OIers 来说是十分方便,查找错误的速度和准确度得到了极大的提升。再也不用痴痴地对着代码干瞪眼。也不用输出中间变量了。
安装
在 cmd 中输入gdb命令,如果输出以下则已安装。
GNU gdb (GDB) 10.2
Copyright (C) 2021 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-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://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)
可以输入q退出。
gdb 一般是 gcc 安装会一起装的。如果没有 gcc,那就去装一个。
如何使用
首先使用g++ -g [文件名] -o [输出的可执行文件名.exe]编译源文件。-g参数表示生成调试信息供 gdb 程序使用。
启动 gdb, 输入gdb [输出的可执行文件名.exe]。