4

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.

4
  • Did you compile with debug enabled ? (gcc option -g) Commented Nov 7, 2011 at 11:48
  • @Offirmo, I haven't checked that. But shouldn't eclipse do it automatically ? If not then where to set this option ? Commented Nov 7, 2011 at 11:49
  • 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. Commented Nov 7, 2011 at 13:20
  • The setting is in "Project -> build configuration -> set active -> debug" Commented Nov 7, 2011 at 13:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

If this is the case then, say even if I am putting a break point at if(i>3) (in your code), still it is not working !
i won't have a value, because the compiler may have been smart enough to just use a register for the value and not map it to any variable in RAM.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.