我不确定我是否正确设置了我的 gtest 环境。当我做简单TEST
的EXPECT_EQ
事情时,一切都很好。然而,当我尝试更高级的东西时TEST_F
,链接器会抱怨。
源代码:
class MyTest : public testing::Test
{
protected:
static const int my_int = 42;
};
TEST_F(MyTest, test)
{
EXPECT_EQ(my_int, 42);
}
这给
Undefined symbols for architecture x86_64:
"MyTest::my_int", referenced from:
MyTest_test_Test::TestBody() in instruction_test.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [tests/tests/run_tests] Error 1
make[2]: *** [tests/tests/CMakeFiles/run_tests.dir/all] Error 2
make[1]: *** [tests/tests/CMakeFiles/run_tests.dir/rule] Error 2
make: *** [run_tests] Error 2
知道为什么会这样吗?
这不是来自 googletest 的问题,而是来自 C++ 的语义。
原因:我们只能在类上调用静态类成员,而不能在类的对象上调用。这是可能的,即使不存在实例。这就是为什么每个静态成员实例都必须初始化的原因,通常在 cpp 文件中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句