I compiled C++ code using the
g++ compiler. The
-g option was specified so that the executable had debugging information. I could run the program using
gdb and place breakpoints and look at the source code. However, when I tried to print the value of a variable in gdb, it threw this strange error:
(gdb) print i No symbol "i" in current context
I tried using Eclipse CDT to compile and debug the same C++ code. Again, I got a similar result. I could run in Debug mode and the program could stop at a breakpoint. However, the Variables window did not show any of the local variables. Hovering the mouse cursor over any local variable did not show its current value!
The culprit turned out to be a mismatch between the compiler and debugger! I had recently switched to GCC 4.8, a newer version. However, I was still using the old version of GDB. In v4.8, GCC produces debugging information in the DWARF4 format, as explained in its release notes. However, this older version of GDB could only understand DWARF3 or versions older than that. It was failing silently when local variables were requested.
The solution was simple: request GCC to produce debugging information in the older format by specifying
-gdwarf-3. Once I did this, GDB could print local variables and they appeared in Eclipse CDT too.
Tried with: GCC 4.8, GDB 7.4 and Ubuntu 12.04 LTS