0

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.

jdodle
  • 141
  • 1
  • 3
  • 11
  • 1
    Just compile with `-O0`, and turn on debugging symbols with `-g` or `-ggdb3`. – cigien Sep 14 '20 at 18:25
  • Debug your code without optimization. – Tony Tannous Sep 14 '20 at 18:26
  • @cigien, @TonyTannous as I said in my post I am not using any optimization flags. gcc defaults to `-O0` but I went ahead and tryed compiling with it just in case and had no change. For completeness, here are all the compiler flags I am using: `-std=c++11 -fPIC -O0 -g -Wall -Og – jdodle Sep 14 '20 at 18:35
  • 2
    Then you'll need to print out the values of the variables. – cigien Sep 14 '20 at 18:37
  • 2
    @jdodle: `-Og` on the command line overrides `-O0` -- you need to use `-O0` AND NO OTHER -O options – Chris Dodd Sep 14 '20 at 19:41

0 Answers0