gtest –– 使用 TEST_F 时未定义的符号

多雾路段

我不确定我是否正确设置了我的 gtest 环境。当我做简单TESTEXPECT_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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用gtest的未定义参考

使用gtest和clang 12编写单元测试时,体系结构x86_64的未定义符号

使用 Makefile 和 GTest 对函数的未定义引用

在gtest中未定义对test :: internal :: EqFailure的引用

通过某些GTest TEST_F的CLion执行挂起,逐步调试会倒退吗?

使用gtest时“已定义_main”

在C ++中使用curl时的未定义符号

尝试使用Cython扩展时的未定义符号

使用g ++时HDF5的未定义符号

使用Boost / Python的未定义符号

使用GTest的C ++多个参数TYPED_TEST

使用objc_setAssociatedObject时,体系结构armv7的未定义符号

在C ++中使用内联方法时出现未定义的符号错误

使用多个文件时出现“架构 x86_64 的未定义符号”错误

使用 dlopen 加载动态库时抛出未定义的符号

使用dlopen时,有关“未定义符号”的共享库错误

为什么我得到“不能在...时使用未定义的值作为符号引用”错误?

导入使用另一个cython模块的cython模块时的未定义符号

使用文件夹结构时的C ++未定义符号

使用ld链接时出现“符号的未定义引用”错误

导入错误:使用 SWIG 时 _...so 文件中的未定义符号

使用简单模板时如何在CLANG中修复“未定义符号”

C ++使用指针时获取体系结构x86_64的未定义符号

在OSX上使用CMake将项目与GLFW3链接时,未定义符号

使用ORKESerializer时体系结构x86_64的未定义符号

使用未定义符号安装 xsane、sane、hplip 等时出错:libusb_set_option 错误

gtest-对`testing :: InitGoogleTest(int *,char **)'的未定义引用

随柯南安装的GTest:未定义参考

GTest链接错误:对'foo :: function'的未定义引用