谷歌测试框架C++单元测试连接错误

8

我是一名初学者C++程序员。我正在尝试在Visual Studio 2012中使用Google Test,并且遇到了从互联网上获取的演示源代码无法启动的问题。

int CompareChar(char* ch1, char* ch2)
{
  if (ch1 == NULL || ch2 == NULL)
  return 88;

  return strcmp(ch1, ch2);
}

TEST(CompareCharTest, InputChar) {

 // Expect equal 
 EXPECT_EQ(0, CompareChar("hello", "hello"));

 // Expect not equal
 EXPECT_NE(0, CompareChar("hello", "world"));
}

TEST(SampleClassTest, InputNumber) {
SampleClass sample(10);
EXPECT_EQ(1, sample.CompareValue(10)); // Expect equal 

}

int _tmain(int argc, _TCHAR* argv[])
{
  ::testing::InitGoogleTest(&argc, argv);
  int i = RUN_ALL_TESTS();
  getchar();
  return i;
}

因此,当我尝试构建代码时,它会生成数十个链接错误,我无法解决。我尝试将.lib文件包含在项目属性中,但无法解决问题。

错误信息:

>1>------ Build started: Project: GTestSample, Configuration: Release Win32 ------
>1>  stdafx.cpp
1>  GTestSample.cpp
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall testing::Message::GetString(void)const " (?GetString@Message@testing@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: __thiscall testing::Message::Message(void)" (??0Message@testing@@QAE@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: static class testing::UnitTest * __cdecl testing::UnitTest::GetInstance(void)" (?GetInstance@UnitTest@testing@@SAPAV12@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: int __thiscall testing::UnitTest::Run(void)" (?Run@UnitTest@testing@@QAEHXZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "protected: __thiscall testing::Test::Test(void)" (??0Test@testing@@IAE@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall testing::Test::~Test(void)" (??1Test@testing@@UAE@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "class testing::TestInfo * __cdecl testing::internal::MakeAndRegisterTestInfo(char const *,char const *,char const *,char const *,void const *,void (__cdecl*)(void),void (__cdecl*)(void),class testing::internal::TestFactoryBase *)" (?MakeAndRegisterTestInfo@internal@testing@@YAPAVTestInfo@2@PBD000PBXP6AXXZ2PAVTestFactoryBase@12@@Z)
1>GTestSample.obj : error LNK2001: unresolved external symbol "void const * __cdecl testing::internal::GetTestTypeId(void)" (?GetTestTypeId@internal@testing@@YAPBXXZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: void __thiscall testing::internal::AssertHelper::operator=(class testing::Message const &)const " (??4AssertHelper@internal@testing@@QBEXABVMessage@2@@Z)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: __thiscall testing::internal::AssertHelper::AssertHelper(enum testing::TestPartResult::Type,char const *,int,char const *)" (??0AssertHelper@internal@testing@@QAE@W4Type@TestPartResult@2@PBDH1@Z)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: __thiscall testing::internal::AssertHelper::~AssertHelper(void)" (??1AssertHelper@internal@testing@@QAE@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "void __cdecl testing::InitGoogleTest(int *,wchar_t * *)" (?InitGoogleTest@testing@@YAXPAHPAPA_W@Z)
1>GTestSample.obj : error LNK2001: unresolved external symbol "bool __cdecl testing::internal::IsTrue(bool)" (?IsTrue@internal@testing@@YA_N_N@Z)
1>GTestSample.obj : error LNK2001: unresolved external symbol "class testing::AssertionResult __cdecl testing::AssertionSuccess(void)" (?AssertionSuccess@testing@@YA?AVAssertionResult@1@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "public: __thiscall testing::AssertionResult::AssertionResult(class testing::AssertionResult const &)" (??0AssertionResult@testing@@QAE@ABV01@@Z)
1>GTestSample.obj : error LNK2001: unresolved external symbol "class testing::AssertionResult __cdecl testing::AssertionFailure(void)" (?AssertionFailure@testing@@YA?AVAssertionResult@1@XZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "class testing::AssertionResult __cdecl testing::internal::EqFailure(char const *,char const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" (?EqFailure@internal@testing@@YA?AVAssertionResult@2@PBD0ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@1_N@Z)
1>GTestSample.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall testing::Test::SetUp(void)" (?SetUp@Test@testing@@MAEXXZ)
1>GTestSample.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall testing::Test::TearDown(void)" (?TearDown@Test@testing@@MAEXXZ)
1>D:\VisualStudioProj\GTestSample\Release\GTestSample.exe : fatal error LNK1120: 19 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我的答案也许可以帮到你:https://stackoverflow.com/a/45901676/264822 - parsley72
2个回答

9

我曾经遇到过同样的问题。请确保在您的测试项目属性中,进入项目->链接器->常规设置,并将msvc/Release目录添加到附加目录中。此外,在链接器->输入下,将gtest.lib添加到附加依赖项中。然后您就可以开始使用了!


2
这个“链接器->输入”部分在我的情况下缺失了,谢谢! - altumano
这个是每次都需要手动完成,还是NuGet包/安装有问题? - helmesjo
我不是很确定,抱歉。 - Oleg Vaskevich
请按照此处的步骤进行操作:https://stackoverflow.com/a/47795243/984471 - Manohar Reddy Poreddy

-2

我知道这篇文章已经很旧了,但它仍然可以帮助到任何人。如上所述,通过快速的谷歌搜索“LNK2019 unresolved external symbol "bool __cdecl testing::internal::IsTrue ..."”,可以找到这个stackoverflow和其他几个给出相同答案的网站。

对于那些尝试了Oleg Vaskevich的解决方案或类似解决方案并且失败了的人,请尝试这个方法。如果您使用Visual Studio,请在C/C++ -> General -> Additional Include Directories中确保包含了gtest。其次,(这可能是您的答案),将gtest/scr/文件夹中的gtest-all.cc复制到项目目录中(无论您的.cpp/.h文件保存在哪里),然后返回Visual Studio并将其包含到您的项目中。您需要取消隐藏文件/文件夹才能查看它。 将其“添加到项目”中。

我知道将其复制粘贴并不适用于每个项目,但重点是至少让它工作,并确保这是真正的问题,而不是其他什么东西。

因为我从来没有编译我的版本,所以一开始我就没有任何lib文件。


5
这个解决方案基本上是错误的,在编程中不应该这样做;它与所有编码原则相矛盾。一个真实的情景是当你只有.lib文件可用而没有源代码时,你的解决方案将不起作用。在这种情况下的解决方案是:1. 在你的项目中包含gmock.lib(或者库的其他名称);2. 如果链接问题出现,改变C/C++->代码生成->运行库->更改为/MTd(因为.lib是构建的)。 - Lucian
确实,正如我在原始陈述中指出的那样,这可能不是最佳做法,但重点是,如果您确实拥有这些文件,请继续将它们复制并粘贴到目录中。您也可以对.lib文件执行此操作。这个解决方案对我有效。一旦我发现我的问题是如何... - Esaith
一旦我发现我的问题是如何不正确地链接(到错误的文件夹级别),我就能够正确地链接了。 这不是为了生产,而是一种调试可能链接错误的方法。 我写了这条可能的路线,因为SO上到目前为止的所有其他解决方案都没有修复我的问题。无论它是否符合'最佳实践',如果它让你更接近实现问题所在并带你走向最终解决方案,那就这样做。您随时可以撤销它。 - Esaith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接