簡體   English   中英

在Visual Studio,Visual C ++ MFC應用程序中使用tinyxml2

[英]Using tinyxml2 with a Visual Studio, Visual C++ MFC Application

我試圖習慣於MFC開發和Visual Studio,因此我已經安裝了Visual Studio 2012 RC並創建了一個簡單的MFC應用程序。 當前,該應用程序僅是為我生成的MFC向導。

我決定要合並一個XML庫,因此我在github上找到了這個庫 我下載了帶有源代碼的ZIP文件,將其解壓縮,然后在Visual Studio中轉到解決方案資源管理器,選擇我的解決方案,右鍵單擊並選擇“添加”>“現有項目”。 我選擇源代碼的項目文件,它顯示在我的“解決方案資源管理器”樹中。

我測試代碼可以編譯。 但是我不太確定如何從當前解決方案中使用它。

我嘗試在我的文檔中使用以下代碼:

#include "../../TinyXML2/leethomason-tinyxml2-a3efec0/tinyxml2.h"

<...snip...>

BOOL LoadDocumentFromXML(const CString& filename) {

    CT2CA pszConvertedAnsiString (filename);
    std::string s(pszConvertedAnsiString);

    tinyxml2::XMLDocument doc(true);
    if (tinyxml2::XML_NO_ERROR != doc.LoadFile(s.c_str())) {
        return FALSE;
    }



    return TRUE;
}

但是,當我嘗試構建項目時,出現此鏈接器錯誤:

------ Build started: Project: GraphApp, Configuration: Debug Win32 ------
  GraphAppDoc.cpp
GraphAppDoc.obj : error LNK2019: unresolved external symbol "public: __thiscall tinyxml2::XMLDocument::XMLDocument(bool)" (??0XMLDocument@tinyxml2@@QAE@_N@Z) referenced in function "int __cdecl LoadDocumentFromXML(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?LoadDocumentFromXML@@YAHABV?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@@Z)
GraphAppDoc.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall tinyxml2::XMLDocument::~XMLDocument(void)" (??1XMLDocument@tinyxml2@@UAE@XZ) referenced in function "int __cdecl LoadDocumentFromXML(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?LoadDocumentFromXML@@YAHABV?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@@Z)
GraphAppDoc.obj : error LNK2019: unresolved external symbol "public: int __thiscall tinyxml2::XMLDocument::LoadFile(char const *)" (?LoadFile@XMLDocument@tinyxml2@@QAEHPBD@Z) referenced in function "int __cdecl LoadDocumentFromXML(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?LoadDocumentFromXML@@YAHABV?$CStringT@_WV?$StrTraitMFC_DLL@_WV?$ChTraitsCRT@_W@ATL@@@@@ATL@@@Z)
C:\Users\Phill\Documents\Visual Studio 2012\Projects\GraphApp\Debug\GraphApp.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

在解決方案資源管理器中,選擇我的MFC應用程序項目,右鍵單擊並選擇“依賴關系”。 我確保將MFC應用程序設置為依賴於tinyxml項目,並且確保“構建順序”正確(首先為tinyxml)。 我也進入“參考文獻...”並在那里也添加tinyxml。 我什至在項目屬性中也將tinyxml的調試目錄添加到MFC應用程序的include路徑中。 我想念什么?

好的,事實是我沒有完全閱讀文檔。 這里閱讀該項目所附的XML指出:

它是一個標頭和一個cpp文件。 只需將它們添加到您的項目中就可以了。

所以我做到了。 然后我得到一些編譯器警告:

------ Build started: Project: GraphApp, Configuration: Debug Win32 ------
  tinyxml2.cpp
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(24): warning C4627: '#include "tinyxml2.h"': skipped when looking for precompiled header use
          Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(26): warning C4627: '#include <cstdio>': skipped when looking for precompiled header use
          Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(27): warning C4627: '#include <cstdlib>': skipped when looking for precompiled header use
          Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(28): warning C4627: '#include <new>': skipped when looking for precompiled header use
          Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(29): warning C4627: '#include <cstddef>': skipped when looking for precompiled header use
          Add directive to 'stdafx.h' or rebuild precompiled header
c:\users\phill\documents\visual studio 2012\projects\graphapp\graphapp\tinyxml2.cpp(1834): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

因此,我在解決方案資源管理器中選擇了“ tinyxml2.cpp”,右鍵單擊,轉到屬性。 在“ C ++”下的屬性中,我轉到“ Precompiled Headers”,並更改了以下選項:

Precompiled Header: Use

Precompiled Header: Not using precompiled headers

然后它神奇地工作了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM