#include <iostream>class CTest{ public: CTest();};CTest::CTest(void){ int x = 0; int y = 4; x = 23;}int main(int argc, char* argv[]){ CTest t; return 0;}
当我用GDB跟到构造函数中时(不管断点打在哪里),p x命令和whatis x命令都显示No symbol "x" in current context. 我编译时是肯定加了调试信息的。另外ubuntu9.04上apt装的gdb没这个问题,vs6和vs2005也没这个问题,开始以为是 版本太低,下了个gdb7.2,问题依旧,求达人解答!
[解决办法] 同时我做了一个对比试验。 huer@ubuntu:~$ c++ -g test.cpp huer@ubuntu:~$ gdb ./a.out GNU gdb (GDB) 7.1-ubuntu Copyright (C) 2010 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". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/huer/a.out...done. (gdb) l CTest::CTest() 4{ 5 public: 6 CTest(); 7}; 8 9CTest::CTest(void) 10{ 11 int x = 0; 12 int y = 4; 13 (gdb) l 14 x = 23; 15y = 0; 16} 17 18int main(int argc, char* argv[]) 19{ 20 CTest t; 21 22 return 0; 23} (gdb) b CTest::CTest() Breakpoint 1 at 0x804859a: file test.cpp, line 11. (2 locations) (gdb) r Starting program: /home/huer/a.out
Breakpoint 1, CTest (this=0xbffff467) at test.cpp:11 11 int x = 0; (gdb) print x $1 = 134514272 (gdb) n 12 int y = 4; (gdb) print x $2 = 0 (gdb) n 14 x = 23; (gdb) n 15y = 0; (gdb) print x $3 = 23 (gdb) n 16} (gdb) print y No symbol "y" in current context. (gdb)