如何使用 Visual Studio 2005 设置 Google C++ 测试框架 (gtest)
网站上没有记录,人们似乎在设置框架时遇到问题.有人可以展示示例项目设置的分步介绍吗?
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?
推荐答案
Arlaharen 所说的基本上是对的,只是他省略了解释链接器错误的部分.首先,您需要构建您的应用程序没有 CRT 作为运行时库.无论如何,您应该始终这样做,因为它确实简化了应用程序的分发.如果您不这样做,那么您的所有用户都需要安装 Visual C++ 运行时库,而那些不安装的用户会抱怨他们的系统上缺少神秘的 DLL……因为链接需要额外花费几百 KB静态地使用 CRT,您以后在支持方面会省去很多麻烦(相信我 - 我已经通过艰难的方式学会了它!).
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 without the 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!).
无论如何,要做到这一点,你去目标的属性 -> C/C++ -> 代码生成 -> 运行时库,它需要为你的发布版本设置为多线程"和多线程"Debug"用于您的调试版本.
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.
由于 gtest 库是以相同的方式构建的,因此您需要确保链接的是 it 的正确版本,否则链接器将拉入运行时库的另一个副本,这是您看到的错误(顺便说一句,如果您使用 MFC,这应该没有区别).您需要将 gtest 构建为调试和发布模式并保留两个副本.然后,您在发布版本中链接 gtest.lib/gtest_main.lib,在调试版本中链接 gtestd.lib/gtest_maind.lib.
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 Release mode 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 头文件的目录(在属性 -> C/C++ -> 常规 -> 附加包含目录中),但如果遇到链接器错误,我假设您已经设法使这部分正确,否则您将首先处理更多的编译器错误.
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.
相关文章