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

我与影子孤独终老i 提交于 2019-12-22 06:42:17

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/28886725/how-to-compile-and-link-google-tests-in-c-project-in-visual-studio-2013-but-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!