2015-10-27 50 views
1
#include "gtest/gtest.h" 

TEST(BattleUnitTest, CountryReturnsProperName) { 
    EXPECT_EQ(1, 1); 
} 

int main(int argc, char* argv[]) { 
    testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

我似乎无法让谷歌测试工作。我运行Nuget包管理器来获取gtest。它不断给我这些错误:不能让谷歌测试工作

Severity Code Description Project File Line 
Error LNK2019 unresolved external symbol "bool __cdecl testing::internal::IsTrue(bool)" ([email protected]@[email protected]@[email protected]) referenced in function "public: void __thiscall testing::internal::scoped_ptr<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::reset(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" ([email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) Assignment1_Battle E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj 1 

Severity Code Description Project File Line 
Error LNK2019 unresolved external symbol "public: __thiscall testing::Message::Message(void)" ([email protected]@@[email protected]) referenced in function "private: virtual void __thiscall BattleUnitTest_CountryReturnsProperName_Test::TestBody(void)" ([email protected][email protected]@EAEXXZ) Assignment1_Battle E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj 1 

Severity Code Description Project File Line 
Error LNK2019 unresolved external symbol "class testing::AssertionResult __cdecl testing::internal::EqFailure(char const *,char const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool)" ([email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) referenced in function "class testing::AssertionResult __cdecl testing::internal::CmpHelperEQ<int,int>(char const *,char const *,int const &,int const &)" ([email protected]@[email protected]@@[email protected]@[email protected]) Assignment1_Battle E:\C++\Projects\Assignment1_Battle\Assignment1_Battle\BattleUnitTest.obj 1 

我可以发布所有的错误,但几乎所有的错误都与LNK2019有关。有谁知道如何解决这些错误?

+0

正如我记得你必须在链接部分的输入字段中添加两个* .lib文件。 –

+0

您正在收到链接器错误,因此您可能需要在项目中包含'gtest'库。看来你只包含了编译器查找头文件的路径。 –

回答

0

看起来你应该对你的测试项目做一些额外的配置。基本上你应该做两个步骤: 1.为你的项目属性添加gtest库名(项目属性 - >配置属性 - >链接器 - >输入)。请注意,您可能需要为Debug和Release配置添加此项功能,并且库名称不同(它们分别是gtest.lib和Release版本,gtestd.lib以及Debug版本;区别在于名称末尾的“d”点)。 2.将gtest库的路径添加到您的项目属性(项目属性 - >配置属性 - > VC++目录 - >库目录)。它也应该分别针对调试和发布配置来完成,因为目录也不同。

如果出现其他问题,则article可能非常有用。它包含完整的场景,可以在不使用Nuget的情况下为Visual Studio中的项目设置gtest。请注意,Nuget基本上将gtest的已编译版本设置为项目/解决方案的“包装”子目录。

0

我有这个问题,但我的连接器设置没有错。我最终追查到这个帖子PrintTo link issue

这确实是由不匹配/ Zc:wchar_t设置引起的。 I 在之前的文章中没有提及我已经将该示例 代码作为Qt控制台应用程序项目(使用Qt插件1.1.9)。 显然,该项目的模板的wchar_t设置设置为“否”,而Google Mock的项目设置默认设置为“yes”。当我将Google Mock的设置重新编译为'否' 时,链接程序问题消失了。

当我将设置“Treat WChar_t As Built in Type”更改为“Yes/Zc:wchar_t”时,构建工作。