C++ Gtest:测试编译错误

C++ Gtest:测试编译错误,c++,g++,googletest,C++,G++,Googletest,我正在尝试测试一个用googletest编写的马达控制库,但我没有编译测试代码。 测试位于名为test.cpp的文件中,如下所示: #include <gtest/gtest.h> #include "../motor.hpp" TEST(constructorTest, contructorDefault) { } 我得到的结果是: main.cpp:8:17: warning: ignoring return value of ‘int RUN_ALL_TESTS()’, d

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

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

}
我得到的结果是:

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

您必须链接正在使用的外部库。仅包含标题是不够的(除非库仅包含标题)。如果这个解决方案不起作用,或者您得到了关于gcc找不到lgtest或GTTest的错误,那么您需要首先安装它(请参阅)

另外,请注意
运行所有测试()
返回一个值,因此您的
main()
应该如下所示:

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

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

    return RUN_ALL_TESTS();
}
#包括
#包括“./motor.hpp”
int main(int argc,char*argv[])
{
::测试::InitGoogleTest(&argc,argv);
返回运行所有测试();
}


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

你没有链接到谷歌测试。将-lgtest添加到compile命令中,并确保您的计算机上安装了google test。谢谢,现在我有另一个错误,我认为它与您所说的外部libs有关。编译器抛出如下错误:“未定义对'pthread_key_create'的引用”。@tul1:我已经更新了答案。请注意,这属于第一部分,你应该自己做一些工作,因为像你的第二部分这样的问题已经有了答案,你真的应该学会如何链接库,而不是等待别人为你做工作……你可能已经(我想)将链接信息放在末尾以使此工作正常。请注意,最可靠的做法是将库放在链接命令行末尾的目标文件之后:
g++main.cpp test.cpp../motor.cpp-o test-lgtest-lpthread
。我假设这个答案在你的平台上起作用;它不会在MacOSX上运行,仅举一个例子。
/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'
#include <gtest/gtest.h>
#include "../motor.hpp"
int main(int argc, char* argv[])

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

    return RUN_ALL_TESTS();
}