[英]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. 我在Windows 7上安装了Microsoft Visual Studio 2013社区版。我想以比下载标题和二进制文件或自行编译更新的方式为C ++安装gtest和gmock。
I found Tools > Nuget Package Manager > Manage Nugets Packages For Solution I chosen Online, then typed gtest. 我找到了Tools> Nuget Package Manager> Manage Nugets Packages for Solution我选择了Online,然后输入gtest。 From search results I've found Google Test, so I've installed its for my current project.
从搜索结果中我发现了Google Test,所以我已经为我当前的项目安装了它。 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
错误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'$分配器@ d @ @@ 2 STD @@@内部@ @@测试QAEXPAV?$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ 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: 所以我打开了:项目属性>配置属性>链接器>输入和我添加:gtest.lib但不幸的是我看到:
Error 1 error LNK1104: cannot open file 'gtest.lib' c:\\Users\\Grzegorz\\documents\\visual studio 2013\\Projects\\tmp\\tmp\\LINK tmp
错误1错误LNK1104:无法打开文件'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
错误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
2013\\Projects\\tmp\\tmp\\msvcprtd.lib(MSVCP120D.dll) tmp
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? 所以我的问题是如何编译和链接谷歌测试在Visual Studio 2013中的C ++项目,但与NuGet包管理器安装Gtest?
Google Test NuGet package contains prebuilt library for static linkage only, but a default initializer for the packet's linkage options is "dynamic". Google Test NuGet包仅包含用于静态链接的预构建库,但数据包的链接选项的默认初始化程序是“动态”。
Project properties
-> Configuration properties
-> Referenced packages
-> gtest
-> Linkage: set to "Static"
And don't add gtest.lib manually. 并且不要手动添加gtest.lib。
I've found a solution: when I typed gtest in nugets package manager in order to find the package I found multiple packages: 我找到了一个解决方案:当我在nugets包管理器中输入gtest以找到包时,我找到了多个包:
so I've installed also FIX8 GTest dependency . 所以我也安装了FIX8 GTest依赖 。 And the linking error disappeared.
并且链接错误消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.