如何使用 Visual Studio 2005 设置 Google C++ 测试框架 (gtest)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/531941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up Google C++ Testing Framework (gtest) with Visual Studio 2005
提问by knaser
It is not documented on the web site and people seem to be having problems setting up the framework. Can someone please show a step-by-step introduction for a sample project setup?
它没有记录在网站上,人们似乎在设置框架时遇到了问题。有人可以展示示例项目设置的分步介绍吗?
采纳答案by Nik Reiman
What Arlaharen said was basically right, except he left out the part which explains your linker errors. First of all, you need to build your application withoutthe CRT as a runtime library. You should always do this anyways, as it really simplifies distribution of your application. If you don't do this, then all of your users need the Visual C++ Runtime Library installed, and those who do not will complain about mysterious DLL's missing on their system... for the extra few hundred kilobytes that it costs to link in the CRT statically, you save yourself a lot of headache later in support (trust me on this one -- I've learned it the hard way!).
Arlaharen 所说的基本上是对的,只是他省略了解释链接器错误的部分。首先,您需要在没有CRT 作为运行时库的情况下构建您的应用程序。无论如何,您应该始终这样做,因为它确实简化了应用程序的分发。如果你不这样做,那么你的所有用户都需要安装 Visual C++ 运行时库,那些不安装的用户会抱怨他们的系统上缺少神秘的 DLL ......静态地使用 CRT,您以后在支持方面会省去很多麻烦(相信我 - 我已经通过艰苦的方式学会了它!)。
Anyways, to do this, you go to the target's properties -> C/C++ -> Code Generation -> Runtime Library, and it needs to be set as "Multi-Threaded" for your Release build and "Multi-Threaded Debug" for your Debug build.
无论如何,要做到这一点,你去目标的属性 -> C/C++ -> 代码生成 -> 运行时库,它需要为你的发布版本设置为“多线程”和为“多线程调试”设置你的调试版本。
Since the gtest library is built in the same way, you need to make sure you are linking against the correct version of it, or else the linker will pull in another copy of the runtime library, which is the error you saw (btw, this shouldn't make a difference if you are using MFC or not). You need to build gtest as both a Debug and Releasemode and keep both copies. You then link against gtest.lib/gtest_main.lib in your Release build and gtestd.lib/gtest_maind.lib in your Debug build.
由于 gtest 库是以相同的方式构建的,因此您需要确保链接的是它的正确版本,否则链接器将拉入运行时库的另一个副本,这是您看到的错误(顺便说一句,这个无论您是否使用 MFC,都不应该有什么不同)。您需要将 gtest 构建为Debug 和 Release模式并保留两个副本。然后,您在发布版本中链接 gtest.lib/gtest_main.lib,在调试版本中链接 gtestd.lib/gtest_maind.lib。
Also, you need to make sure that your application points to the directory where the gtest header files are stored (in properties -> C/C++ -> General -> Additional Include Directories), but if you got to the linker error, I assume that you already managed to get this part correct, or else you'd have a lot more compiler errors to deal with first.
此外,您需要确保您的应用程序指向存储 gtest 头文件的目录(在属性 -> C/C++ -> 常规 -> 附加包含目录中),但如果遇到链接器错误,我假设您已经设法使这部分正确,否则您将首先处理更多的编译器错误。
回答by mtlynch
(These instructions get the testing framework working for the Debug configuration. It should be pretty trivial to apply the same process to the Release configuration.)
(这些说明使测试框架适用于 Debug 配置。将相同的过程应用于 Release 配置应该非常简单。)
Get Google C++ Testing Framework
获取 Google C++ 测试框架
- Download the latest gtest framework
- Unzip to
C:\gtest
- 下载最新的gtest 框架
- 解压到
C:\gtest
Build the Framework Libraries
构建框架库
- Open
C:\gtest\msvc\gtest.sln
in Visual Studio - Set Configuration to "Debug"
- Build Solution
C:\gtest\msvc\gtest.sln
在 Visual Studio 中打开- 将配置设置为“调试”
- 构建解决方案
Create and Configure Your Test Project
创建和配置您的测试项目
- Create a new solution and choose the template Visual C++ > Win32 > Win32 Console Application
- Right click the newly created project and choose Properties
- Change Configuration to Debug.
- Configuration Properties > C/C++ > General > Additional Include Directories: Add
C:\gtest\include
- Configuration Properties > C/C++ > Code Generation > Runtime Library: If your code links to a runtime DLL, choose Multi-threaded Debug DLL (/MDd). If not, choose Multi-threaded Debug (/MTd).
- Configuration Properties > Linker > General > Additional Library Directories: Add
C:\gtest\msvc\gtest\Debug
orC:\gtest\msvc\gtest-md\Debug
, depending on the location of gtestd.lib - Configuration Properties > Linker > Input > Additional Dependencies: Add
gtestd.lib
- 创建一个新的解决方案并选择模板 Visual C++ > Win32 > Win32 Console Application
- 右键单击新创建的项目并选择属性
- 将配置更改为调试。
- 配置属性 > C/C++ > 常规 > 附加包含目录:添加
C:\gtest\include
- 配置属性 > C/C++ > 代码生成 > 运行时库:如果您的代码链接到运行时 DLL,请选择多线程调试 DLL (/MDd)。如果没有,请选择多线程调试 (/MTd)。
- 配置属性>链接器>常规>附加库目录:添加
C:\gtest\msvc\gtest\Debug
或C:\gtest\msvc\gtest-md\Debug
,取决于gtestd.lib的位置 - 配置属性 > 链接器 > 输入 > 附加依赖项:添加
gtestd.lib
Verifying Everything Works
验证一切正常
- Open the cpp in your Test Project containing the
main()
function. Paste the following code:
#include "stdafx.h" #include <iostream> #include "gtest/gtest.h" TEST(sample_test_case, sample_test) { EXPECT_EQ(1, 1); } int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); RUN_ALL_TESTS(); std::getchar(); // keep console window open until Return keystroke }
Debug > Start Debugging
- 在包含该
main()
函数的测试项目中打开 cpp 。 粘贴以下代码:
#include "stdafx.h" #include <iostream> #include "gtest/gtest.h" TEST(sample_test_case, sample_test) { EXPECT_EQ(1, 1); } int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); RUN_ALL_TESTS(); std::getchar(); // keep console window open until Return keystroke }
调试 > 开始调试
If everything worked, you should see the console window appear and show you the unit test results.
如果一切正常,您应该会看到控制台窗口出现并显示单元测试结果。
回答by Armando
I did a video tutorial about the setup: http://www.youtube.com/watch?v=mzSzwQOmMRs
我做了一个关于设置的视频教程:http: //www.youtube.com/watch?v=mzSzwQOmMRs
回答by philipper
If you don t want to write your own main() for tests you can use the main() function defined in gtest_main.lib but then you get linker errors "Entry point must be defined" in VS2012. In your test-project set ProjectProperties->Linker->System->SubSystem to "Console" as this will force VS2012 to look for an entry point called "main()" and will find it in gtest_main.lib (provided you ve linked it in properly).
如果您不想为测试编写自己的 main(),您可以使用 gtest_main.lib 中定义的 main() 函数,但随后您会在 VS2012 中收到链接器错误“必须定义入口点”。在您的测试项目中,将 ProjectProperties->Linker->System->SubSystem 设置为“Console”,因为这将强制 VS2012 寻找名为“main()”的入口点,并将在 gtest_main.lib 中找到它(前提是您已链接它正确)。
回答by Arlaharen
Having built gtest, this is what I have done:
构建 gtest 后,这就是我所做的:
- Add \mypath\gtest-1.0.1\Debug (or Release) to Common Properties->Linker->General->Additional Library Directories
- Add gtest.lib and gtest_main.lib to Common Properties->Linker->Input->Additional Dependencies
- 将\mypath\gtest-1.0.1\Debug(或Release)添加到Common Properties->Linker->General->Additional Library Directories
- 将 gtest.lib 和 gtest_main.lib 添加到 Common Properties->Linker->Input->Additional Dependencies
After that I just write my tests using TEST or TEST_F as appropriate and compile them together with my main function:
之后,我只是根据需要使用 TEST 或 TEST_F 编写我的测试,并将它们与我的主函数一起编译:
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
回答by Jinuk Kim
In Microsoft Visual Studio, misconfigured runtime library type causes link errors.
在 Microsoft Visual Studio 中,错误配置的运行时库类型会导致链接错误。
VS 2005(and 2008) uses Multithreaded DLL or Multithreaded Debug DLL as default. But Google Test library uses Mulithreaded or Mulithreaded debug runtime as default.
VS 2005(和 2008)默认使用多线程 DLL 或多线程调试 DLL。但是 Google 测试库默认使用多线程或多线程调试运行时。
So, choose appropriate run time library type for google test library. (in Configuration properties -> Code Generation -> Runtime Library).
因此,为 google 测试库选择合适的运行时库类型。(在配置属性 -> 代码生成 -> 运行时库中)。
回答by Manohar Reddy Poreddy
Many errors, took a while to fix.
很多错误,花了一些时间来修复。
Here are simple steps:
以下是简单的步骤:
# Download Googletest ZIP file from: https://github.com/google/googletest
# 从以下位置下载 Googletest ZIP 文件:https: //github.com/google/googletest
# Googletest visual studio solution
# Googletest 视觉工作室解决方案
Open C:\googletest\googletest\msvc10\gtest.sln
Change "Solution configuration"
from "Debug" to "Release"
Build Solution
creates gtest.lib
# Your project:
# 你的项目:
Project Properties > Configuration Properties > VC++ Directories > Include Directories
append the following: ;C:\googletest\googletest\include
Project Properties > Configuration Properties > Linker > General > Additional Library Directories >
append the following: ;C:\googletest\googletest\msvc10\gtest\Win32-Release
Project Properties > Configuration Properties > C/C++ > Runtime Library
Remove the value of the above. (or match it to Googletest project)