用 gdb 调试 C/C++ 程序

本文介绍了如何在 Linux 下使用 gdb 调试 C/C++ 程序。通过一个简单的示例,演示了从编译带有调试信息的程序,到启动 gdb,设置断点,查看源代码,单步执行,打印变量值等基本调试步骤。gdb 的常用命令如 `list`、`break`、`next` 和 `finish` 等也在文中提及。
摘要由CSDN通过智能技术生成
 

        作者:zieckey (zieckey@yahoo.com.cn)
        All Rights Reserved!
       
       
     Linux 下有一个叫 gdb 的 GNU 调试程序. gdb 是一个用来调试 C 和 C++ 程序的强有力调试器. 它使你能在程序运行时观察程序的内部结构和内存的使用情况. 以下是 gdb 所提供的一些功能:

    * 它使你能监视你程序中变量的值.
    * 它使你能设置断点以使程序在指定的代码行上停止执行.
    * 它使你能一行行的执行你的代码.

    在命令行上键入 gdb 并按回车键就可以运行 gdb 了, 如果一切正常的话, gdb 将被启动并且你将在屏幕上看到类似的内容:

[root@localhost root]# gdb
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
(gdb)


    当你启动 gdb 后, 你能在命令行上指定很多的选项.
你也可以以下面的方式来运行 gdb :
gdb <fname>
例如,
[root@localhost exam-source-file]# gdb a.out
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
(gdb)

下面我们以一个简单的例子来开始我们的gdb调试之旅。
一个调试示例

源程序:testgbd.c

// name: testgbd.c
// This file is used to test how to use gdb to debug a c program .It is very simple ! Enjoy yourself!
// Author : zieckey
// data : 2006/11/13


#include <stdio.h>

int accumulate( int n )
{
    int sum = 0 , i ;
   
    for( i=1; i<=n; i++)
    {
        sum += i;
    }
   
    return sum;
}


int main( void )
{
    int i , n ;
    int result ;
   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值