gpt4 book ai didi

c++ - Gtest : test compiling error

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:19:21 26 4
gpt4 key购买 nike

我正在尝试测试我用 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 标志来编译测试。

最佳答案

尝试:

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

您必须链接您正在使用的外部库。包含 header 是不够的(除非库仅包含 header )。如果此解决方案不起作用,或者您收到有关 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();
}

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

关于c++ - Gtest : test compiling error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com