使用tinyxml2与Visual Studio, Visual c++ MFC应用程序。

时间:2022-11-23 00:38:26

I'm trying to get accustomed to both MFC development and Visual Studio so I have installed Visual Studio 2012 RC and have created a simple MFC Application. Currently the application is little more than the MFC Wizard generated for me.

我试着习惯了MFC开发和Visual Studio,所以我安装了Visual Studio 2012 RC,并创建了一个简单的MFC应用程序。目前,应用程序只是为我生成的MFC向导。

I decided I wanted to incorporate an XML Library so I found this one on github. I download the ZIP file with the source code, unzip it and then in Visual Studio I goto the solution explorer, choose my solution, right click and choose "Add" > "Existing Project". I select the project file for the source code and it appears in my Solution Explorer tree.

我决定要合并一个XML库,所以我在github上找到了这个。我用源代码下载ZIP文件,解压缩,然后在Visual Studio中找到解决方案资源管理器,选择我的解决方案,右键单击并选择“添加”>“现有项目”。我选择了源代码的项目文件,它出现在我的解决方案资源管理器树中。

I test the code compiles and it does. However I'm not quite sure how to use it from my current solution.

我测试代码编译,它确实做到了。然而,我不太确定如何从我现在的解决方案中使用它。

I try to use this code in my doc:

我试着在我的文档中使用这段代码:

#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;
}

However I get this linker error when I try to build the project:

但是,当我试图构建项目时,我得到了这个链接错误。

------ 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 ==========

In the solution explorer I select my MFC application project, right click and choose "Dependencies". I make sure the MFC app is set to be dependent on tinyxml project and I ensure the "Build Order" is correct (tinyxml first). I also goto into "References..." and add tinyxml there too. I even add the debug directory of tinyxml to my MFC app's include path in project properties as well. What am I missing please?

在解决方案资源管理器中,我选择了MFC应用程序项目,右击并选择“依赖项”。我确保MFC应用程序将依赖tinyxml项目,我确保“构建顺序”是正确的(tinyxml first)。我还会进入“引用”,并在那里添加tinyxml。我甚至将tinyxml的调试目录添加到MFC应用程序中,包括项目属性中的路径。我错过了什么?

1 个解决方案

#1


8  

Ok it turns out I didn't read the documentation fully. Reading the XML attached the project here states:

好的,原来我没有完全读过文档。阅读XML附加的项目:

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

它是一个头文件和一个cpp文件。只要把这些添加到你的项目中,就可以了。

So I did this. Then I got some compiler warnings:

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

------ 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 ========== 

So then I selected "tinyxml2.cpp" in the solutions explorer, right clicked, went to properties. In properties under "C++" I went to "Precompiled Headers" and changed the option that read:

然后我选择了tinyxml2。在“解决方案资源管理器”中,右键单击,进入属性。在“c++”下的属性中,我转到“预编译头”,并更改了该选项:

Precompiled Header: Use

to

Precompiled Header: Not using precompiled headers

Then it magically worked!

然后它奇迹般地成功了!

#1


8  

Ok it turns out I didn't read the documentation fully. Reading the XML attached the project here states:

好的,原来我没有完全读过文档。阅读XML附加的项目:

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

它是一个头文件和一个cpp文件。只要把这些添加到你的项目中,就可以了。

So I did this. Then I got some compiler warnings:

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

------ 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 ========== 

So then I selected "tinyxml2.cpp" in the solutions explorer, right clicked, went to properties. In properties under "C++" I went to "Precompiled Headers" and changed the option that read:

然后我选择了tinyxml2。在“解决方案资源管理器”中,右键单击,进入属性。在“c++”下的属性中,我转到“预编译头”,并更改了该选项:

Precompiled Header: Use

to

Precompiled Header: Not using precompiled headers

Then it magically worked!

然后它奇迹般地成功了!