GTEST:测试编译错误

问题描述:

我想测试一个电机控制的lib我已经写了googletest但我没去过编译测试的代码。 该测试是在一个名为TEST.CPP文件,如以下几点:GTEST:测试编译错误

#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(); 
} 

编译我excecuted以下行:

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 

我是什么d错了吗?

编辑

样子@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标志来编译测试。

+0

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

尝试:

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(); 
} 

我已经更新了答案(检查2号线),以支付你的下一个问题。这和以前一样,你真的应该开始找到你的问题的答案,而不是只是要求和等待有人为你做所有的工作。

+0

谢谢,现在我有另一个错误,我认为它与你说的外部库相关。编译器会抛出像这样的错误:“未定义的对pthread_key_create的引用”。 – tul1

+1

@ tul1:我已经更新了我的答案。请注意,这属于第一个,你应该自己做一些工作,因为已经回答了像第二部分这样的问题,你应该学会如何链接图书馆,而不是等待某人为你工作...... – RippeR

+0

有(我认为)把链接信息放在最后来完成这项工作。 – JC1