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

前端 未结 9 1205
攒了一身酷
攒了一身酷 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:27

    The compiler is not allowed to optimiza away global objects.
    Even if they are never used.

    Somthing else is happening in your code.
    Now if you built a static library with your global object and that global object is not referenced from the executable it will not be pulled into the executable by the linker.

提交回复
热议问题