最近升级到 g++ 4.8.1 后,我发现在 gdb 中调试是完全不可能的。g++ 似乎隐藏了 gdb 中的所有变量,无论优化选项如何。在接下来的会话中,runner.cpp 如下:
#include <vector>
using namespace std;
int main(void) {
vector<int> arr;
int a = 3;
int b = 2;
b = a + 3;
arr.push_back(1);
arr.push_back(2);
arr.push_back(3);
arr.push_back(4);
return 0;
}
这是结果:
Script started on Tue 14 Jul 2015 01:11:14 PM PDT
me@ministation:~/Development/clib$ g++ -g -O0 runner.cpp
me@ministation:~/Development/clib$ gdb -q ./a.out
Reading symbols from /home/me/Development/clib/a.out...done.
(gdb) break 11
Breakpoint 1 at 0x40095c: file runner.cpp, line 11.
(gdb) run
Starting program: /home/me/Development/clib/a.out
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000
Breakpoint 1, main () at runner.cpp:11
11 arr.push_back(1);
(gdb) print a
$1 = {i = {0, 1045149306}, d = 1.2904777690891933e-08} ## I have no idea what this means
(gdb) print b
$2 = {i = {0, 1068498944}, d = 0.0625}
(gdb) print arr
No symbol "arr" in current context.
(gdb) info locals
No locals.
(gdb) next
12 arr.push_back(2);
(gdb)
13 arr.push_back(3);
(gdb) print arr
No symbol "arr" in current context.
(gdb) next
14 arr.push_back(4);
(gdb)
16 return 0;
(gdb) print arr
No symbol "arr" in current context.
(gdb) q
A debugging session is active.
Inferior 1 [process 6392] will be killed.
Quit anyway? (y or n) y
me@ministation:~/Development/clib$
Script done on Tue 14 Jul 2015 01:12:05 PM PDT
我看过一些类似的帖子,其中推荐了 -O0 标志,但它似乎在这里不起作用。使用 g++4.6 编译后的完全相同的会话会产生预期的结果。关于 g++4.8 的原因有什么想法吗?