在Visual Studio 2012中使用Google Test

55

我正在尝试开始进行单元测试。我下载了最新版本的gtest并将其解压到A:\gtest。根据说明,我打开了gtest.sln。Visual Studio只有在我同意更新时才允许我打开sln文件。然后我尝试构建,但是出现了一堆错误:

1>------ Build started: Project: gtest, Configuration: Debug Win32 ------
1>  gtest-all.cc
1>a:\gtest\include\gtest\gtest-printers.h(550): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\gtest-printers.h(558): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4017): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>          a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4249) : see reference to class template instantiation 'testing::internal::CartesianProductGenerator9<T1,T2,T3,T4,T5,T6,T7,T8,T9>' being compiled
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4017): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4017): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4017): error C2955: 'testing::internal::ParamGeneratorInterface' : use of class template requires template argument list
1>          a:\gtest\include\gtest\internal\gtest-param-util.h(142) : see declaration of 'testing::internal::ParamGeneratorInterface'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4019): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4030): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4030): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4035): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4035): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4042): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>          a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4042) : see reference to class template instantiation 'testing::internal::CartesianProductGenerator9<T1,T2,T3,T4,T5,T6,T7,T8,T9>::Iterator' being compiled
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4042): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4042): error C2955: 'testing::internal::ParamIteratorInterface' : use of class template requires template argument list
1>          a:\gtest\include\gtest\internal\gtest-param-util.h(69) : see declaration of 'testing::internal::ParamIteratorInterface'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4044): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4044): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4077): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4077): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4119): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4119): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4123): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4123): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4204): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4204): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4256): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>          a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4505) : see reference to class template instantiation 'testing::internal::CartesianProductGenerator10<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10>' being compiled
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4256): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4256): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4256): error C2955: 'testing::internal::ParamGeneratorInterface' : use of class template requires template argument list
1>          a:\gtest\include\gtest\internal\gtest-param-util.h(142) : see declaration of 'testing::internal::ParamGeneratorInterface'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4258): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4270): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4270): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4275): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4275): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4282): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>          a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4282) : see reference to class template instantiation 'testing::internal::CartesianProductGenerator10<T1,T2,T3,T4,T5,T6,T7,T8,T9,T10>::Iterator' being compiled
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4282): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4282): error C2955: 'testing::internal::ParamIteratorInterface' : use of class template requires template argument list
1>          a:\gtest\include\gtest\internal\gtest-param-util.h(69) : see declaration of 'testing::internal::ParamIteratorInterface'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4284): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4284): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4320): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4320): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4366): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4366): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4370): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4370): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4456): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4456): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4738): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>          a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4766) : see reference to class template instantiation 'testing::internal::CartesianProductHolder9<Generator1,Generator2,Generator3,Generator4,Generator5,Generator6,Generator7,Generator8,Generator9>' being compiled
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4738): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4738): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4782): error C2977: 'std::tuple' : too many template arguments
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
1>          a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4813) : see reference to class template instantiation 'testing::internal::CartesianProductHolder10<Generator1,Generator2,Generator3,Generator4,Generator5,Generator6,Generator7,Generator8,Generator9,Generator10>' being compiled
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4782): error C3203: 'tuple' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
1>a:\gtest\include\gtest\internal\gtest-param-util-generated.h(4782): error C2955: 'std::tuple' : use of class template requires template argument list
1>          b:\program files (x86)\microsoft visual studio 2012\vc\include\utility(73) : see declaration of 'std::tuple'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我是不是做错了什么,还是这是VS2012的问题?我不依赖于Google Test,并且不排斥使用其他框架,如果它与VS更好地配合使用,那么我只是听说gtest非常好用。

4个回答

79

VC++ 2012不支持可变模板参数(variadic templates),而且也永远不会支持。因此,其标准库实现尝试使用预处理器生成的重载和特化来模拟它们。正如这篇博客所述,假定的可变模板参数数量默认为5 - 问题在于gtest尝试使用多达10个模板参数来实例化std::tuple<>

正如博客中提到的,您可以将_VARIADIC_MAX宏定义为项目范围内的最大参数数目,以支持多达10个参数;您可能希望通过项目的预处理器定义设置来完成此操作,但要确保在包含任何stdlib头文件(直接或间接)之前,每个编译单元都定义了该宏。


谢谢您的回复。我现在正在尝试。 - user1599559
抱歉如果这是一个愚蠢的问题,但是我该如何在整个项目中定义_VARIADIC_MAX?我有一种感觉它应该在项目属性->C++->预处理器->预处理器定义中。但是我对语法不太确定。 - user1599559
3
如果您单击预处理器定义的下拉箭头,会出现一个名为“<Edit...>”的条目 - 点击它,然后添加一行,只需输入“_VARIADIC_MAX”,然后点击“确定”。 您需要为每个项目配置(调试/发布、x86/x64等)执行此操作。 - ildjarn
我已经这样做了,现在我收到错误消息 fatal error C1189: #error : _VARIADIC_MAX 必须介于5和10之间(包括5和10) - user1599559
12
@Kyryx: 啊,抱歉,我忘记我们的目标是将其设置为10。;-] 将代码行_VARIADIC_MAX=10修改为此即可。 - ildjarn
没关系,非常感谢你的帮助。现在它已经完美构建了。 - user1599559

8

不要使用_VARIADIC_MAX宏修改VS2012元组实现,我通过在gtest项目和包含gtest头文件的项目中定义以下两个宏来解决这个问题:

GTEST_HAS_TR1_TUPLE=0
GTEST_USE_OWN_TR1_TUPLE=1

GTEST_HAS_TR1_TUPLE设置为0可以防止gtest尝试使用STL头文件中的tuple类,将GTEST_USE_OWN_TR1_TUPLE设置为1会指示gtest使用自己的tuple实现而不是省略需要tuple支持的功能。我个人更喜欢这个选项来调整STL实现,这个解决方案应该适用于任何有类似问题的编译器。


6

仍然在r683版本中出现故障。 - chappjc

1
此外,在获取最新版本之前,请将GTEST_USE_OWN_TR1_TUPLE=0添加到您的预处理器定义中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接