【问题标题】:can not print global objects in gdb无法在 gdb 中打印全局对象
【发布时间】:2016-11-26 16:35:13
【问题描述】:

我有这个简单的 c++ 代码:

#include<bits/stdc++.h>

using namespace std;
vector<string> q;

int main()
{
    q.push_back("test1");
    q.push_back("test2");
    cout<<q.front();
    return 0;
}

当我使用gdb 打印变量q 时出现以下错误:

No symbol "q" in current context.

我像这样使用 g++ 编译我的程序:

 g++ -g a.cpp

这是我的 gdb 命令:

gdb a.out 
GNU gdb (GDB) 7.12
Copyright (C) 2016 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 "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) b 6
Breakpoint 1 at 0x400c6f: file a.cpp, line 6.
(gdb) r
Starting program: /home/mohammad/Desktop/a.out 

Breakpoint 1, main () at a.cpp:9
9       q.push_back("test");
(gdb) print q
No symbol "q" in current context.

【问题讨论】:

  • 可能您在同一目录中缺少符号文件?
  • 题外话:谨慎使用#include&lt;bits/stdc++.h&gt;using namespace std;,并极其谨慎地使用它们。在此处阅读更多信息 Why is “using namespace std” considered bad practice?Why should I not #include <bits/stdc++.h>?
  • 可能是因为q 是全球性的。查找print 的详细信息。一分钟后回来
  • 还是不知道为什么看不到,但是如果缩小q的作用域static vector&lt;string&gt; q;就可以看到了。
  • @user4581301 你有没有想过为什么全局变量没有显示在 gdb 中?

标签: c++ gdb global-variables


【解决方案1】:

这个问题与新的 gcc Dual ABI introduced in gcc 5 有关。 gdb 对这些新的 C++11 ABI 标记的支持很差。例如,查看这些错误:

作为一种解决方法,您可以通过使用-D_GLIBCXX_USE_CXX11_ABI=0 编译来禁用这个新的 gcc ABI:

g++ -D_GLIBCXX_USE_CXX11_ABI=0 -g a.cpp

【讨论】:

  • 或者,(gdb) p 'q[abi:cxx11]'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 2018-03-01
  • 2012-09-03
  • 1970-01-01
相关资源
最近更新 更多