【问题标题】:Gtest: test compiling errorGtest:测试编译错误
【发布时间】:2015-12-02 09:57:22
【问题描述】:

我正在尝试测试我用 googletest 编写的电机控制库,但我没有编译测试的代码。 测试位于名为 test.cpp 的文件中,如下所示:

#include <gtest/gtest.h>
#include "../motor.hpp"
TEST(constructorTest, contructorDefault)
{

}

我将测试主函数放在另一个名为 main.cpp 的文件中。

#include <gtest/gtest.h>
#include "../motor.hpp"
int main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc,argv);
    RUN_ALL_TESTS();
}

为了编译,我执行了以下行:

g++ main.cpp test.cpp ../motor.cpp -o test

我得到的结果是:

main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result]
  RUN_ALL_TESTS();
                 ^
/tmp/ccZ5BaBH.o: In function `main':
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
/tmp/ccZ5BaBH.o: In function `RUN_ALL_TESTS()':
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'
/tmp/ccFuAMp3.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x5c): undefined reference to `testing::internal::GetTestTypeId()'
test.cpp:(.text+0x84): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::constructorTest_contructorDefault_Test()':
test.cpp:(.text._ZN38constructorTest_contructorDefault_TestC2Ev[_ZN38constructorTest_contructorDefault_TestC5Ev]+0x14): undefined reference to `testing::Test::Test()'
/tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x20): undefined reference to `testing::Test::SetUp()'
/tmp/ccFuAMp3.o:(.rodata._ZTV38constructorTest_contructorDefault_Test[_ZTV38constructorTest_contructorDefault_Test]+0x28): undefined reference to `testing::Test::TearDown()'
/tmp/ccFuAMp3.o: In function `constructorTest_contructorDefault_Test::~constructorTest_contructorDefault_Test()':
test.cpp:(.text._ZN38constructorTest_contructorDefault_TestD2Ev[_ZN38constructorTest_contructorDefault_TestD5Ev]+0x1f): undefined reference to `testing::Test::~Test()'
/tmp/ccFuAMp3.o:(.rodata._ZTI38constructorTest_contructorDefault_Test[_ZTI38constructorTest_contructorDefault_Test]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: error: ld returned 1 exit status

如果我删除编译行的 test.cpp,我会得到另一个结果:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, declared with attribute warn_unused_result [-Wunused-result]
  RUN_ALL_TESTS();
                 ^
/tmp/cc61r6NU.o: In function `main':
main.cpp:(.text+0x1e): undefined reference to `testing::InitGoogleTest(int*, char**)'
/tmp/cc61r6NU.o: In function `RUN_ALL_TESTS()':
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0x5): undefined reference to `testing::UnitTest::GetInstance()'
main.cpp:(.text._Z13RUN_ALL_TESTSv[_Z13RUN_ALL_TESTSv]+0xd): undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status

我做错了什么?

编辑

看起来@RippeR 说的是对的,但现在我收到以下错误:

/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_getspecific'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_key_delete'
/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../lib/libgtest.so: undefined reference to `pthread_setspecific'

我必须包含其他内容吗?

解决方案 问题是添加 -lpthread 标志来编译测试。

【问题讨论】:

  • 您没有链接到 Google 测试。将 -lgtest 添加到您的编译命令中,并确保您的计算机上安装了 google 测试。

标签: c++ g++ googletest


【解决方案1】:

试试:

g++ main.cpp test.cpp ../motor.cpp -o test -lgtest -lpthread

您必须链接您正在使用的外部库。包括标头是不够的(除非库是仅标头)。如果此解决方案不起作用,或者您收到有关 gcc 找不到 lgtest 或 gtest 的错误,那么您需要先安装它(请参阅here)。

另外,请注意 RUN_ALL_TESTS(); 返回一个值,因此您的 main() 应如下所示:

#include <gtest/gtest.h>
#include "../motor.hpp"
int main(int argc, char* argv[])

{
    ::testing::InitGoogleTest(&argc,argv);

    return RUN_ALL_TESTS();
}

我已经更新了答案(检查第二行)以涵盖您的下一个问题。这和以前一样,你真的应该开始寻找问题的答案,而不是仅仅要求和等待有人为你做所有的工作。

【讨论】:

  • 谢谢,现在我有另一个错误,我认为它与您所说的外部库有关。编译器会抛出类似这样的错误:“undefined reference to `pthread_key_create'”。
  • @tul1:我已经更新了我的答案。请注意,这属于第一个,您应该自己做一些工作,因为已经回答了像您的第二部分这样的问题,您真的应该学习如何链接库而不是等待有人为您工作......
  • 您可能需要(我认为)将链接信息放在最后才能完成这项工作。
  • 请注意,最好 - 最可靠 - 将库放在链接命令行的末尾,在目标文件之后:g++ main.cpp test.cpp ../motor.cpp -o test -lgtest -lpthread。我假设这个答案中的内容在您的平台上有效;它不会在 Mac OS X 上,仅举一个例子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多