1

我正在尝试习惯 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 应用程序的项目属性中的包含路径中。请问我错过了什么?

4

1 回答 1

9

好的,事实证明我没有完全阅读文档。阅读此处附有项目的 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 Header: Use

Precompiled Header: Not using precompiled headers

然后它神奇地起作用了!

于 2012-06-23T21:59:50.487 回答