2015-10-27 49 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」時,構建工作。