使用GDB时遇到的问题

在网上看到一个使用GDB实验的例子,如下:

 

#include  <stdio.h>

main ()

{  

char my_string[] = "hello there";

  my_print (my_string);  

my_print2 (my_string);

}

void my_print (char *string)

{

  printf ("The string is %s/n", string);

}

void my_print2 (char *string)

{  

char *string2;  

int size, i;

size = strlen (string);

  string2 = (char *) malloc (size + 1);  

for (i = 0; i < size; i++)   

  string2[size - i] = string[i];  

string2[size+1] = `/0';

  printf ("The string printed backward is %s/n", string2);

}  

不管它叫greeting ,还是test,我就就叫它test.c

我编译:gcc -o test test.c

期间于warning,关于strlen和malloc的

然后执行:./test

结果为;

The string is hello there
The string printed backward is

有错了,于是开始使用GDB来调试了。

# gdb test
GNU gdb 6.8-debian
Copyright (C) 2008 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 "i486-linux-gnu"...
(gdb) file gdb
Load new symbol table from "/home/yaozhangjun/programme/gdb"? (y or n) y
Reading symbols from /home/yaozhangjun/programme/test...done.

//gdb内的输出和在 gdb 外面运行的结果一样

(gdb) run
Starting program: /home/yaozhangjun/programme/test
The string is hello there
The string printed backward is

 

Program exited with code 040.

然后开始了list,可是问题来了,第一次LIST

(gdb) list
1    init.c: No such file or directory.
    in init.c

第二次LIST

(gdb)list

1 in init.c

第三次LIST

(gdb)list

1 in init.c

好家伙,什么也没有。

又从网上搜得要编译的时候 -g,以为这下有救了,可是。。。

# gcc -g test -o test.c
gcc: test: No such file or directory
gcc: no input files

怎么会这样呢?

 

原来要这样做   gcc -o test -g test.c   才可以,

终于见到

(gdb) list
1    #include <stdio.h>
2    main()
3    {
4    char my_string[]="hello there";
5    my_print(my_string);
6    my_print2(my_string);
7    }
8   
9    void my_print(char *string)
10    {

I love them.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值