4
votes

I have used Eclipse for Java coding without any problem. With C++ Eclipse (Indigo), my problem is that, I am not able to see the values of the variables when I put the mouse over them ! It just shows the definition of the variable.

In Java if we right click on a variable then we get an option like "Inspect value". That option is also not visible in C++ eclipse. How to solve this issue ? Is there any plugin or configuration am I missing ?

Have freshly installed Ubuntu 11.10 in Virtual Box (Windows XP Host). Then installed g++ 4.6, Eclipse Indigo and Eclipse CDT. In "Debug Configurations", it shows:

Debugger: gdb/mi
Advanced: Automatically track values of "Variables" and "Registers"
GDB Debugger: gdb
GDB command file: .gdbinit
GDB Command set: Standard(Linux)
Protocol: mi
(unchecked) Verbose console mode
(unchecked) Use full file path to set breakpoints

I am able to put break points and stop the execution there, the only problem is seeing values.

2
Did you compile with debug enabled ? (gcc option -g) - Offirmo
@Offirmo, I haven't checked that. But shouldn't eclipse do it automatically ? If not then where to set this option ? - iammilind
I just tried a default "hello world" project in eclipse, and indeed debug options are activated by default. Check for you by opening the console window, doing "build > clean" then "build project" and check for a -g option (or -g3). This way we'll be sure. - Offirmo
The setting is in "Project -> build configuration -> set active -> debug" - Offirmo

2 Answers

1
votes

Most likely, due to optimizations those values don't exist at that point. For example, consider:

int foo(void)
{
    int i=SomeFunctions();
    if(i>3) Foo();
    else
    {
       Bar();
       Baz();
    }
    // breakpoint here
    return 8;
}

The compiler is free to keep i entirely in a register. By the time you hit the breakpoint, that register may have been re-used for some other purpose. It can be impossible for the debugger to tell you the value of i at that point.

As a general rule, unless the execution of a program depends on the value of a variable, there is no requirement that it be possible to determine that variable's value.

Turning off optimizations can help.

1
votes

It seems possible that the compiler is optimizing it out. A quick trick to make sure a variable will never be optimized out is to declare it as volatile. This tells the compiler that the variable should be treated like it could changed at any time (such as a global being modified from an interrupt).

Example:

int main()
{
   // Even though we never read the value of test, it will not be optimized away
   volatile int test;

   test = foo();

   return 0;
}