用gdb调试基于参数的C程序

Debug argument based C program with gdb

本文关键字:参数 程序 gdb 调试 于参数      更新时间:2023-10-16

我有一个c++程序,我通过传递字符串来运行它。

g++ -o a main.cpp -lpthread

并用./a "Good nice" 执行

但是我如何用gdb调试它呢?main.cpp从包含在其中的其他文件调用函数。

gdb ./a "Good nice"

把"--"作为文件,并说没有这样的文件!

我想逐行调试!

使用gdb:的--args选项

gdb --args ./a "Good nice"

还要将-g选项添加到编译器调用中,因为否则gdb将无法将可执行文件与源代码连接起来:

g++ -g -o a main.cpp -lpthread

使用没有参数的gdb

gdb ./a

然后在gdb中,在运行程序之前

set args "Good nice"

你可以看到你设置了什么参数,使用

show args

请参阅此处了解详细信息。

gdb/prog->设置参数string->运行

选择在run 之后提供参数

$gdb ./a
 run "Good nice"