8

I'm a beginner C++ programmer. I'm trying Google Test in Visual Studio 2012 and have problem starting a demo source code I got from 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;
}

So, when I try to build the code, it generates dozens of linking errors which I couldn't sort out. I tried to include .lib files in the property of the project but couldn't solve it.

The error:

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

2 Answers 2

9

I had the same issue. Make sure that in your test project's properties, you go into Project->Linker->General settings and add the msvc/Release directory to Additional Directories. Furthermore, under Linker->Input, add gtest.lib into Additional Dependencies. Then you should be good to go!

4
-2

I know this post is old, but it could still help anyone out. A quick Google search for "LNK2019 unresolved external symbol "bool __cdecl testing::internal::IsTrue ..." as stated above gives this stackoverflow and several sites with the same answers.

For those that tried Oleg Vaskevich solution or similar and failed, try this. If you are using Visual Studio, C/C++ -> General -> Additional Include Directories and make sure that the gtest is included. Secondly, (which will likely be your answer), copy the gtest-all.cc in the gtest/scr/ folder and paste in your project directory (wherever your .cpp/.h files are saved), then go back to Visual Studio and include it in your project. You will have to unhide files/folders to view it. 'Include to project'.

I know that copying and pasting this isn't ideal for every project, but the point is to at least get it working and making sure this is the true problem and not something else.

Because I never complied my version, I never had any lib files to begin with.

3
  • 5
    This solution is fundamentally incorrect, you should not do such things in programming; it contradicts all coding principles. A real scenario is when you have only the .lib available and not the sources, your solution will not work. The solution in this case is: 1. include the gmock.lib (or whatever name the library has) in your project and 2. if linkage problems with the gmock.lib change C/C++->Code Generation->Runtime Library->change to /MTd (as the .lib is build)
    – Lucian
    Nov 2, 2015 at 9:18
  • It is true that this may not be the best practice, as I had pointed out in my original statement, but the point is if you do have the files - go ahead and copy and paste them right into the directory. You can do this with .lib files as well. This solution worked for me. Once I found that my problem was how I
    – Esaith
    Nov 4, 2015 at 17:42
  • Once I found that my problem was how I was incorrectly linking (to the wrong folder level), I was able to link correctly. This isn't for production but a way to debug possible linking errors. I wrote this one possible route as all other solutions so far on SO did not fix my problem. Regardless of whether it is 'best practice' or not, if it brings you a step closer to realizing where the problem is and brings you to a final solution, so be it. You can always undo it later.
    – Esaith
    Nov 4, 2015 at 17:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.