visual-studio - 将 tinyxml2 与 Visual Studio、Visual

我正在努力适应 MFC 开发和 Visual Studio,因此我安装了 Visual Studio 2012 RC 并创建了一个简单的 MFC 应用程序。目前,该应用程序只不过是为我生成的 MFC 向导。

我决定加入一个 XML 库,所以我找到了 this one on 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 应用程序的项目属性中的包含路径中。请问我错过了什么?

最佳答案

好吧,事实证明我没有完整阅读文档。读取项目附带的 XML here状态:

It is one header and one cpp file. Simply add these to your project and off you go.

所以我这样做了。然后我收到了一些编译器警告:

------ 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++”下的属性中,我转到“预编译 header ”并更改了以下选项:

Precompiled Header: Use

Precompiled Header: Not using precompiled headers

然后它神奇地起作用了!

关于visual-studio - 将 tinyxml2 与 Visual Studio、Visual C++ MFC 应用程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11110444/

相关文章:

jquery - 获取 "removeattr is not a function"异常

architecture - CQRS:查询端的业务逻辑

wordpress - 使用帖子 ID 查询帖子

css - 可缩放字体大小,SASS (LESS) with "px"vs plain ems

jsf - 如何为 h :graphicImage Primefaces 添加操作

jquery - 如何使用 jQuery 和 HTML5 canvas 画线

sql-server - 当命令类型为 adCmdText 时是否可以使用命名参数?

debugging - vb6 如何在 msgbox 中进入 Debug模式

css - 使用 :nth-child(odd) selector on div image

asp.net - 如何在网址栏中隐藏 asp 网址?