Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and link google tests in C++ project in Visual Studio 2013 but with Gtest installed by NuGet Package Manager?

I have Microsoft Visual Studio 2013 Community Edition on Windows 7. I want to install gtest and gmock for C++ in newer way than downloading headers and binaries or compiling by myself.

I found Tools > Nuget Package Manager > Manage Nugets Packages For Solution I chosen Online, then typed gtest. From search results I've found Google Test, so I've installed its for my current project. After the installation the code below compiles:

#include <iostream>
#include <gtest\gtest.h>

using namespace std;
using namespace ::testing;

int factorian(int n)
{
    if (n == 0 || n == 1)
        return 1;
    else
        return n*factorian(n - 1);
}

TEST(factorianTest, simpleTest)
{
    ASSERT_EQ(1, factorian(0));
    ASSERT_EQ(1, factorian(1));
    ASSERT_EQ(2, factorian(2));
}

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

but it doesn't link:

Error 1 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::allocator > >::reset(class std::basic_string,class std::allocator > *)" (?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) c:\Users\Grzegorz\documents\visual studio 2013\Projects\tmp\tmp\Source.obj tmp

So I opened: Project Properties>Configuration Properties>Linker>Input and I added: gtest.lib but unfortunately I see:

Error 1 error LNK1104: cannot open file 'gtest.lib' c:\Users\Grzegorz\documents\visual studio 2013\Projects\tmp\tmp\LINK tmp

when I add full path to file (I know that I shouldn't) there are strange errors:

Error 2 error LNK2005: "public: __thiscall std::_Container_base12::_Container_base12(void)" (??0_Container_base12@std@@QAE@XZ) already defined in gtestd.lib(gtest-all.obj) c:\Users\Grzegorz\documents\visual studio

2013\Projects\tmp\tmp\msvcprtd.lib(MSVCP120D.dll) tmp

so my question is how to compile and link google tests in C++ project in Visual Studio 2013 but with Gtest installed by NuGet Package Manager?

like image 377
baziorek Avatar asked Mar 05 '15 20:03

baziorek


2 Answers

Google Test NuGet package contains prebuilt library for static linkage only, but a default initializer for the packet's linkage options is "dynamic".

Project properties 
-> Configuration properties 
-> Referenced packages 
-> gtest 
-> Linkage: set to "Static"

And don't add gtest.lib manually.

like image 147
den_po Avatar answered Nov 02 '22 13:11

den_po


I've found a solution: when I typed gtest in nugets package manager in order to find the package I found multiple packages:

  1. Google Test
  2. FIX8 GTest Dependency Symbols
  3. FIX8 GTest dependency

so I've installed also FIX8 GTest dependency. And the linking error disappeared.

like image 39
baziorek Avatar answered Nov 02 '22 11:11

baziorek