Chinaunix首页 | 论坛 | 博客
  • 博客访问: 537610
  • 博文数量: 80
  • 博客积分: 1496
  • 博客等级: 上尉
  • 技术积分: 1292
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-18 11:24
个人简介

IT码农一个~

文章分类

全部博文(80)

文章存档

2020年(3)

2019年(7)

2017年(1)

2016年(2)

2015年(2)

2014年(26)

2013年(26)

2012年(2)

2011年(1)

2010年(1)

2008年(9)

我的朋友

分类: LINUX

2013-09-07 12:31:59

今天在家里装了台虚拟机,系统装的CentOS 6.4, gcc 版本4.8.1,gdb是系统带的 7.2-60

随便写了个测试程序,本意是想测gdb printer 对std标准容器的打印支持的
#include
#include
#include
#include

using namespace std;

int main()
{
    vector   vec;
    for(int i=1; i < 10; i++)
        vec.push_back(i*i);

    unordered_map>  un_map;
    un_map.insert(make_pair(1, vec));

    map  id_name_map;
    id_name_map[1] = "json";
    id_name_map[99] = "trash";

    return 0;
}


编译的时候也加了 -g选项,可是用gdb 打印变量的值的时候
都是显示 No symbol "i" in current context, 其他变量也都无法打印。

后来在网上查的,
加上 参数 -gstabs+
g++ -g -Wall -gstabs+ -std=c++11 -o gdb_printer test_gdb_printer.cpp 

然后再进入 gdb 就可以查看所有变量了。 
这个我还不清楚为什么? 难道是gcc版本编译的时候什么选项没打开? 

以下是gcc手册 对几种 -g的解释:
-g Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.

On most systems that use stabs format, `-g' enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but will probably make other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use `-gstabs+', `-gstabs', `-gxcoff+', `-gxcoff', `-gdwarf-1+', or `-gdwarf-1' (see below).

Unlike most other C compilers, GCC allows you to use `-g' with `-O'. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values were already at hand; some statements may execute in different places because they were moved out of loops.

Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.

The following options are useful when GCC is generated with the capability for more than one debugging format.


-ggdb Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF 2, stabs, or the native format if neither of those are supported), including GDB extensions if at all possible.


-gstabs Produce debugging information in stabs format (if that is supported), without GDB extensions. This is the format used by DBX on most BSD systems. On MIPS, Alpha and System V Release 4 systems this option produces stabs debugging output which is not understood by DBX or SDB. On System V Release 4 systems this option requires the GNU assembler.


-gstabs+ Produce debugging information in stabs format (if that is supported), using GNU extensions understood only by the GNU debugger (GDB). The use of these extensions is likely to make other debuggers crash or refuse to read the program.
阅读(7121) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

lincolnrainbow2014-02-13 11:32:21

后来换了编译选项后,此问题解决。初步判断是编译选项导致的,具体哪个当时是使用系统自带4.4的选项,判断两个版本的编译选项略有不同。