如何避免 C++ 编译器优化器删除静态变量代码?

How to avoid for c++ compiler optimizer to remove static variable code?

我在 MS Visual Studio C++ 2013 sp5 中使用 C++。 如您所知,google test(gtest) 将测试代码初始化为静态变量。 当我将这些 gtest 代码 (.cpp) 放在主项目中时,没有问题。 但是,当我将这些 gtest 代码 (.cpp) 移动到主项目所依赖的库项目时,gtest 不起作用。 我不能像没有代码一样设置断点。 (断点点看起来像白色圆圈而不是红色圆圈)。 我猜编译器优化器在 cpp 中省略了这些静态变量代码,因为这些变量无处引用。但是,它的构造函数做了一些事情,所以我不明白为什么编译器删除或不为这些静态变量编写代码。

如何预防这些情况? 有什么选择吗?或者有什么诀窍吗?

把'Link Library Dependencies'和'Use Library Dependency Inputs'变成Yes然后编译器生成代码。有用。 C++ static variable in .lib does not initialize force visual studio to link all symbols in a lib file