c++ - 如何在 Visual Studio 2013 的 C++ 项目中编译和链接谷歌测试,但使用 NuGet 包管理器安装 Gtest?

标签 c++ visual-studio linker nuget googletest

我在 Windows 7 上安装了 Microsoft Visual Studio 2013 Community Edition。 我想以比下载头文件和二进制文件或自己编译更新的方式为 C++ 安装 gtest 和 gmock。

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

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

但它没有链接:

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 Properties>Configuration Properties>Linker>Input 我补充说: gtest.lib 但不幸的是我看到:

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

当我添加文件的完整路径时(我知道我不应该这样做)出现奇怪的错误:

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

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

最佳答案

Google Test NuGet 包包含仅用于静态链接的预构建库,但数据包链接选项的默认初始化程序是“动态”。

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

并且不要手动添加 gtest.lib。

关于c++ - 如何在 Visual Studio 2013 的 C++ 项目中编译和链接谷歌测试,但使用 NuGet 包管理器安装 Gtest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28886725/

相关文章:

c++ - 返回类的新实例

c# - 您可以拆分一个 visual studio 项目 (c#) 并引用它吗?

c++ - 检测共享库中变量的重复定义

c - 相当于 LD_PRELOAD 的 gcc 标志?

c++ - 重新声明纯虚函数

c++ - 如何在遍历的同时添加要设置的元素?

c++ - 在 clang 插件中,如何查看编译翻译单元是否出错?

visual-studio - Settings.settings中的:"The method or operation is not implemented."错误

c# - 如何检测 Visual Studio 中未处理的异常?

c++ - 对 `function_name' 的 undefined reference