I have seen a lot of other questions asking this same thing and every single one suggests declaring the variable every possible combination of static volatile
or turning off optimizations using #pragma
or __attribute__
. In my case, I am not using any optimization flags during compilation and using volatile
is not possible since the value is passed to functions of other libraries that do not use volatile
and casting it away is not allowed (throws compiler error, for good reason).
So I am looking for an alternative solution to this problem. Why do I want to do this? I think like most other people asking this question I am trying to inspect some code with gdb and the value I am interested in is being optimized out.
One thought I have is that there may be a standard C/C++ function I could call that would prevent the optimization from taking place. Unfortunately I don't know enough about how values are optimized out to know what functions might do this.
NOTE: I say C/C++ because I am compiling with C++ but am wrapping some of my code in extern "C"
blocks. In particular, my main
is C++ and I am using some shared libraries of which 2 are compile as C and the other is compiled as C++ with extern "C"
blocks.