Can't get google test to work

2019-08-10 04:12发布

问题:

#include "gtest/gtest.h"

TEST(BattleUnitTest, CountryReturnsProperName) {
    EXPECT_EQ(1, 1);
}

int main(int argc, char* argv[]) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

I can't seem to get google test to work. I ran Nuget package manager to get gtest. It keeps giving me these errors:

Severity    Code    Description Project File    Line
Error   LNK2019 unresolved external symbol "bool __cdecl testing::internal::IsTrue(bool)" (?IsTrue@internal@testing@@YA_N_N@Z) referenced in function "public: void __thiscall testing::internal::scoped_ptr<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::reset(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (?reset@?$scoped_ptr@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@internal@testing@@QAEXPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) Assignment1_Battle  E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj    1

.

    Severity    Code    Description Project File    Line
Error   LNK2019 unresolved external symbol "public: __thiscall testing::Message::Message(void)" (??0Message@testing@@QAE@XZ) referenced in function "private: virtual void __thiscall BattleUnitTest_CountryReturnsProperName_Test::TestBody(void)" (?TestBody@BattleUnitTest_CountryReturnsProperName_Test@@EAEXXZ)    Assignment1_Battle  E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj    1

.

Severity    Code    Description Project File    Line
Error   LNK2019 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) referenced in function "class testing::AssertionResult __cdecl testing::internal::CmpHelperEQ<int,int>(char const *,char const *,int const &,int const &)" (??$CmpHelperEQ@HH@internal@testing@@YA?AVAssertionResult@1@PBD0ABH1@Z) Assignment1_Battle  E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj    1

I could post all the errors but almost all of them have to do with LNK2019. Does anyone know how to fix these errors?

回答1:

I had this problem but there was nothing wrong with my linker settings. I eventually tracked it down to this post PrintTo link issue:

This was indeed caused by the mismatching /Zc:wchar_t setting. I failed to mention in the previous post that I had made that example code as Qt Console Application project (with Qt Add-in 1.1.9). Apparently the wchar_t setting is set to 'no' with that project template, while Google Mock's project setting is set to 'yes' by default. When I recompiled Google Mock with the setting set to 'no' the linker problem went away.

When I changed my setting "Treat WChar_t As Built in Type" to "Yes /Zc:wchar_t" the build worked.



回答2:

Looks like you need some additional configuration of your test project:

  1. Add gtest library name to you project properties (Project Properties->Configuration Properties->Linker->Input). Be aware that you probably need to add this thing for both Debug and Release configurations and that library names are different (they are gtest.lib for Release and gtestd.lib for Debug; the difference is "d" at the end of the name before dot).
  2. Add the path to the gtest library to your project properties (Project Properties->Configuration Properties->VC++ Directories->Library Directories). This also needs to be done separately for Debug and Release configurations, because the directories are different as well.

If something else goes wrong, this article might be really useful. It contains the full scenario of setting up gtest for a project in Visual Studio without using Nuget. Just be aware that Nuget basically sets up already compiled version of gtest to "package" sub-directory of your project/solution.