我在使用Google Test时遇到了麻烦。在Ubuntu 17.04上,我使用命令构建了Google Test:
sudo aptitude install libgtest-dev
cd /usr/src/gtest
sudo cmake -DBUILD_SHARED_LIBS=ON .
sudo make
sudo mv libgtest* /usr/lib
然后我尝试编译非常简单的测试文件:
#include <gtest/gtest.h>
#include <iostream>
int foo()
{
return 4;
}
TEST(TestFoo, test1)
{
ASSERT_EQ(foo(), 4);
}
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
并获得以下错误:
$ g++ -lgtest -o simplesample simplesample.cc
/tmp/ccZB2V5i.o: In function `TestFoo_test1_Test::TestBody()':
simplesample.cc:(.text+0x77): undefined reference to `testing::Message::Message()'
simplesample.cc:(.text+0xa6): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
...
/tmp/ccZB2V5i.o:(.data.rel.ro._ZTV18TestFoo_test1_Test[_ZTV18TestFoo_test1_Test]+0x20): undefined reference to `testing::Test::SetUp()'
/tmp/ccZB2V5i.o:(.data.rel.ro._ZTV18TestFoo_test1_Test[_ZTV18TestFoo_test1_Test]+0x28): undefined reference to `testing::Test::TearDown()'
/tmp/ccZB2V5i.o: In function `TestFoo_test1_Test::~TestFoo_test1_Test()':
simplesample.cc:(.text._ZN18TestFoo_test1_TestD2Ev[_ZN18TestFoo_test1_TestD5Ev]+0x22): undefined reference to `testing::Test::~Test()'
/tmp/ccZB2V5i.o:(.data.rel.ro._ZTI18TestFoo_test1_Test[_ZTI18TestFoo_test1_Test]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: error: ld returned 1 exit status