debug optimizing away variables

Hello,

Any hints on how to prevent g++ from optimizing away my variables? Or at least I am assuming they are being optimized away, any hints/clues would be appreciated. For example:

int main(){
int n=0;
int a=3;
cout<<a;
return 0;
}

If this were my real program, then while debugging using insight I would not be able to see what n was holding by hovering over it or even putting a watch on it. While using the below compile command on the code above it works, it does not work in my actual app's script. Any guesses? Thanks in advance.

Simple compile statement that worked:
g++ joe.C -o joe.o -g -O0

Compile statement that ends up in variables gone:
g++ test.o -o test /liba.o /libb.o /libc.a -lsicl -lm -g -O0
Declaring the variable volatile will prevent the compiler from storing the value in a register. I would not recommend using this as general practice; only for debug purposes, and then change it back to non-volatile when done debugging.
Thanks for the tip, I'll add it to the toolbag. Still though, there's got to be a way to force the compiler to retain all of the debugging info. Any other suggestions?
The compiler is free to optimize away any variables provided that:
1) They are not declared "volatile";
2) No code takes their address;
3) It is a POD-type that can be stored in a register.

"free to optimize away any variables"

Even with the debug "-g" flag and no optimization "-O" flags?

If it is a POD-type, is there an easy way to determine which register it is stored in and then look at it?
I was speaking from a Standards point of view. What GCC does is outside that discussion.

No, there isn't.

Oh well, it sounds like I may be stuck. If anyone has anymore suggestions, please let me know. Thanks in advance.
Topic archived. No new replies allowed.