How do I prevent my 'unused' global variables being compiled out?

前端 未结 9 1171
攒了一身酷
攒了一身酷 2020-12-10 00:42

I\'m using static initialisation to ease the process of registering some classes with a factory in C++. Unfortunately, I think the compiler is optimising out the \'unused\'

相关标签:
9条回答
  • 2020-12-10 01:31

    To build off of Arthur Ulfeldt, volatile tells the compiler that this variable can change outside of the knowledge of the compiler. I've used it for put a statement to allow the debugger to set a breakpoint. It's also useful for hardware registers that can change based on the environment or that need a special sequence. i.e. Serial Port receive register and certain watchdog registers.

    0 讨论(0)
  • 2020-12-10 01:38

    Are you using gcc with gdb? There was a problem in the past where gdb could not accurately set breakpoints in constructors.

    Also, are you using an optimization level which allows the compiler to inline methods in the class definition.

    0 讨论(0)
  • 2020-12-10 01:41

    You need to use -whole-archive when linking. See the answer here:

    ld linker question: the --whole-archive option

    0 讨论(0)
提交回复
热议问题