如何编译和链接谷歌测试在Visual Studio 2013中的C ++项目,但与Gtest安装NuGet包管理器? [英] How to compile and link google tests in C++ project in Visual Studio 2013 but with Gtest installed by NuGet Package Manager?

查看:1360
本文介绍了如何编译和链接谷歌测试在Visual Studio 2013中的C ++项目,但与Gtest安装NuGet包管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7上有Microsoft Visual Studio 2013社区版。
我要以比下载标题和二进制文件或自己编译更新的方式安装gtest和gmock。

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.

我找到工具> Nuget包管理器>管理Nugets包解决方案
我选择了在线,然后键入gtest。从搜索结果我发现了Google测试,所以我已经为我的当前项目安装了它。
安装后,下面的代码编译:

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();
}

但未链接:


错误1错误LNK2019:未解析的外部符号bool __cdecl
testing :: internal :: IsTrue(bool)(?IsTrue @ internal @ testing @@ YA_N_N @ Z)
引用函数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

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

所以我打开:Project属性>配置属性>链接器>输入
和我添加:
gtest.lib
但不幸的是我看到:

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


错误1错误LNK1104:无法打开文件
'gtest.lib'c:\Users\Grzegorz\documents\visual studio
2013\Projects\tmp\tmp \LINK tmp

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

当我添加文件的完整路径时(我知道我不应该)有奇怪的错误: p>

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


错误2错误LNK2005:public:__thiscall
std :: _ Container_base12 :: _ Container_base12(void)
?0_Container_base12 @ std @@ QAE @ XZ)已在
gtestd.lib(gtest-all.obj)中定义c:\Users\Grzegorz\documents\visual studio

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

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


所以我的问题是如何编译和链接谷歌测试在Visual Studio 2013中的C ++项目,但是Gtest安装NuGet包管理器?

推荐答案

我找到了一个解决方案:
当我在nugets包管理器中键入gtest为了找到包我找到了多个软件包:

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测试

  2. FIX8 GTest依赖符号

  3. FIX8 GTest依赖

所以我也安装了 FIX8 GTest依赖。而且链接错误消失了。

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

这篇关于如何编译和链接谷歌测试在Visual Studio 2013中的C ++项目,但与Gtest安装NuGet包管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆