【问题标题】:Googletest C++ unit testing linking error [duplicate]Googletest C ++单元测试链接错误[重复]
【发布时间】:2013-11-24 22:17:29
【问题描述】:

我是一名初学者 C++ 程序员。我正在 Visual Studio 2012 中尝试 Google 测试,但在启动从 Internet 获得的演示源代码时遇到问题。

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 ==========

【问题讨论】:

标签: c++ unit-testing visual-studio-2012 googletest


【解决方案1】:

我知道这篇文章很旧,但它仍然可以帮助任何人。如上所述,在 Google 上快速搜索“LNK2019 unresolved external symbol”bool __cdecl testing::internal::IsTrue ...”会给出这个 stackoverflow 和几个具有相同答案的站点。

对于那些尝试过 Oleg Vaskevich 解决方案或类似解决方案但失败的人,试试这个。如果您使用 Visual Studio,C/C++ -> 常规 -> 附加包含目录 并确保包含 gtest。其次,(这可能是你的答案),复制 gtest/scr/ 文件夹中的 gtest-all.cc 并粘贴到你的项目目录中(无论你的 .cpp/.h 文件保存在哪里),然后返回 Visual Studio并将其包含在您的项目中。您必须取消隐藏文件/文件夹才能查看它。 '包括到项目'。

我知道复制和粘贴它并不适合每个项目,但关键是至少让它工作并确保这是真正的问题而不是其他问题。

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

【讨论】:

  • 这个解决方案根本不正确,你不应该在编程中做这样的事情;它与所有编码原则相矛盾。一个真实的情况是,当您只有可用的 .lib 而没有源时,您的解决方案将不起作用。这种情况下的解决方案是:1. 在您的项目中包含 gmock.lib(或库具有的任何名称)和 2. 如果 gmock.lib 的链接问题更改 C/C++->Code Generation->Runtime Library->更改为 /MTd(构建 .lib)
  • 确实,这可能不是最佳实践,正如我在原始声明中指出的那样,但关键是如果您确实拥有这些文件 - 继续将它们复制并粘贴到目录。您也可以使用 .lib 文件执行此操作。这个解决方案对我有用。一旦我发现我的问题是我如何
  • 一旦我发现我的问题是我如何错误地链接(到错误的文件夹级别),我就能够正确链接。这不是用于生产,而是一种调试可能的链接错误的方法。我写了一条可能的路线,因为到目前为止所有其他解决方案都没有解决我的问题。不管它是否是“最佳实践”,如果它能让你更接近于意识到问题所在并让你找到最终的解决方案,那就这样吧。您以后可以随时撤消它。
【解决方案2】:

我有同样的问题。确保在测试项目的属性中,进入 Project->Linker->General 设置并将msvc/Release 目录添加到Additional Directories。此外,在 Linker->Input 下,将 gtest.lib 添加到 Additional Dependencies 中。那么你应该很高兴!

【讨论】:

  • 这个“链接器->输入”部分在我的情况下丢失了,谢谢!
  • 是否应该每次都手动完成,还是nuget包/安装有问题?
  • 我不太确定,抱歉。
  • 按照此处的步骤操作:stackoverflow.com/a/47795243/984471
猜你喜欢
  • 2023-03-26
  • 2014-08-12
  • 2011-09-10
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
  • 2015-08-20
  • 2014-06-20
相关资源
最近更新 更多