4

I have this simple c++ code :

#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;
}

When I use gdb to print variable q I get following error:

No symbol "q" in current context.

I compile my program using g++ like this:

 g++ -g a.cpp

And here is my gdb commands:

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.
Mohammad Razeghi
  • 1,574
  • 2
  • 14
  • 33
  • Probably you're missing a symbol file at the same directory? – Dean Seo Nov 26 '16 at 16:50
  • Off topic: use `#include` and `using namespace std;`with caution and use them together with extreme caution. Read more here [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Nov 26 '16 at 17:00
  • Probably because `q` is global. Looking up the specifics of `print`. Back in a minute – user4581301 Nov 26 '16 at 17:03
  • Still not sure why it can't be seen, but if you reduce `q`'s scope `static vector q;` it can be seen. – user4581301 Nov 26 '16 at 17:25
  • @user4581301 did you ever figure out why global variables are not showing in gdb? – borizzzzz Apr 21 '19 at 17:45

1 Answers1

5

This issue is somehow related to new gcc Dual ABI, introduced in gcc 5. gdb has poor support of these new C++11 ABI tags. See these bugs for example:

As a workaround you may disable this new gcc ABI by compiling with -D_GLIBCXX_USE_CXX11_ABI=0:

g++ -D_GLIBCXX_USE_CXX11_ABI=0 -g a.cpp
ks1322
  • 33,961
  • 14
  • 109
  • 164