使用TinyXML(链接库?在c ++中)

时间:2010-04-26 16:13:51

标签: c++ tinyxml

+ ...您好 我是新手......我不知道如何在c ++中包含外部库。 这太难了。

我想使用TinyXML。 所以我做了这个:

example2.cpp

#include <iostream>
#include "tinyxml.h"


void write_app_settings_doc( )  
{  
    TiXmlDocument doc;  
    TiXmlElement* msg;
    TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
    doc.LinkEndChild( decl );  

    TiXmlElement * root = new TiXmlElement( "MyApp" );  
    doc.LinkEndChild( root );  

    TiXmlComment * comment = new TiXmlComment();
    comment->SetValue(" Settings for MyApp " );  
    root->LinkEndChild( comment );  

    TiXmlElement * msgs = new TiXmlElement( "Messages" );  
    root->LinkEndChild( msgs );  

    msg = new TiXmlElement( "Welcome" );  
    msg->LinkEndChild( new TiXmlText( "Welcome to MyApp" ));  
    msgs->LinkEndChild( msg );  

    msg = new TiXmlElement( "Farewell" );  
    msg->LinkEndChild( new TiXmlText( "Thank you for using MyApp" ));  
    msgs->LinkEndChild( msg );  

    TiXmlElement * windows = new TiXmlElement( "Windows" );  
    root->LinkEndChild( windows );  

    TiXmlElement * window;
    window = new TiXmlElement( "Window" );  
    windows->LinkEndChild( window );  
    window->SetAttribute("name", "MainFrame");
    window->SetAttribute("x", 5);
    window->SetAttribute("y", 15);
    window->SetAttribute("w", 400);
    window->SetAttribute("h", 250);

    TiXmlElement * cxn = new TiXmlElement( "Connection" );  
    root->LinkEndChild( cxn );  
    cxn->SetAttribute("ip", "192.168.0.1");
    cxn->SetDoubleAttribute("timeout", 123.456); // floating point attrib

    doc.SaveFile( "appsettings.xml" );  
} 


int main()
{
    write_app_settings_doc( );
  return 0;
}

只是一个示例文件。 所以..当我用以下代码编译时:

  

g ++ example2.cpp

我收到以下错误消息:

/tmp/cc6npUsW.o: In function `write_app_settings_doc()':
example2.cpp:(.text+0x13): undefined reference to `TiXmlDocument::TiXmlDocument()'
example2.cpp:(.text+0x43): undefined reference to `TiXmlDeclaration::TiXmlDeclaration(char const*, char const*, char const*)'
example2.cpp:(.text+0x58): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x8f): undefined reference to `TiXmlElement::TiXmlElement(char const*)'
example2.cpp:(.text+0xa4): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x112): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x132): undefined reference to `TiXmlElement::TiXmlElement(char const*)'
example2.cpp:(.text+0x147): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x17e): undefined reference to `TiXmlElement::TiXmlElement(char const*)'
example2.cpp:(.text+0x1cb): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x1f4): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x214): undefined reference to `TiXmlElement::TiXmlElement(char const*)'
example2.cpp:(.text+0x261): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x28a): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x2aa): undefined reference to `TiXmlElement::TiXmlElement(char const*)'
example2.cpp:(.text+0x2bf): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x2f6): undefined reference to `TiXmlElement::TiXmlElement(char const*)'
example2.cpp:(.text+0x30b): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x33d): undefined reference to `TiXmlElement::SetAttribute(char const*, char const*)'
example2.cpp:(.text+0x358): undefined reference to `TiXmlElement::SetAttribute(char const*, int)'
example2.cpp:(.text+0x373): undefined reference to `TiXmlElement::SetAttribute(char const*, int)'
example2.cpp:(.text+0x38e): undefined reference to `TiXmlElement::SetAttribute(char const*, int)'
example2.cpp:(.text+0x3a9): undefined reference to `TiXmlElement::SetAttribute(char const*, int)'
example2.cpp:(.text+0x3c9): undefined reference to `TiXmlElement::TiXmlElement(char const*)'
example2.cpp:(.text+0x3de): undefined reference to `TiXmlNode::LinkEndChild(TiXmlNode*)'
example2.cpp:(.text+0x40d): undefined reference to `TiXmlElement::SetAttribute(char const*, char const*)'
example2.cpp:(.text+0x42a): undefined reference to `TiXmlElement::SetDoubleAttribute(char const*, double)'
example2.cpp:(.text+0x43d): undefined reference to `TiXmlDocument::SaveFile(char const*) const'
/tmp/cc6npUsW.o: In function `TiXmlString::operator=(char const*)':
example2.cpp:(.text._ZN11TiXmlStringaSEPKc[TiXmlString::operator=(char const*)]+0x23): undefined reference to `TiXmlString::assign(char const*, unsigned int)'
/tmp/cc6npUsW.o: In function `TiXmlString::quit()':
example2.cpp:(.text._ZN11TiXmlString4quitEv[TiXmlString::quit()]+0xc): undefined reference to `TiXmlString::nullrep_'
/tmp/cc6npUsW.o: In function `TiXmlComment::TiXmlComment()':
example2.cpp:(.text._ZN12TiXmlCommentC1Ev[TiXmlComment::TiXmlComment()]+0x15): undefined reference to `TiXmlNode::TiXmlNode(TiXmlNode::NodeType)'
example2.cpp:(.text._ZN12TiXmlCommentC1Ev[TiXmlComment::TiXmlComment()]+0x1e): undefined reference to `vtable for TiXmlComment'
/tmp/cc6npUsW.o: In function `TiXmlText::TiXmlText(char const*)':
example2.cpp:(.text._ZN9TiXmlTextC1EPKc[TiXmlText::TiXmlText(char const*)]+0x17): undefined reference to `TiXmlNode::TiXmlNode(TiXmlNode::NodeType)'
example2.cpp:(.text._ZN9TiXmlTextC1EPKc[TiXmlText::TiXmlText(char const*)]+0x20): undefined reference to `vtable for TiXmlText'
example2.cpp:(.text._ZN9TiXmlTextC1EPKc[TiXmlText::TiXmlText(char const*)]+0x4f): undefined reference to `TiXmlNode::~TiXmlNode()'
/tmp/cc6npUsW.o: In function `TiXmlDocument::~TiXmlDocument()':
example2.cpp:(.text._ZN13TiXmlDocumentD1Ev[TiXmlDocument::~TiXmlDocument()]+0xb): undefined reference to `vtable for TiXmlDocument'
example2.cpp:(.text._ZN13TiXmlDocumentD1Ev[TiXmlDocument::~TiXmlDocument()]+0x24): undefined reference to `TiXmlNode::~TiXmlNode()'
collect2: ld returned 1 exit status

我主要是一名java开发人员,我在c ++中遇到很多使用外部库的问题。 我不知道什么时候联系。何时加入。当我必须制作-I / myincludepath /

这些文件位于同一文件夹中:

  

example.cpp tinyxml.h tinyxml.cpp   tinystr.h tinystr.cpp

有人能帮助我吗?

5 个答案:

答案 0 :(得分:6)

尝试类似:

g++ example2.cpp tinyxml.cpp tinystr.cpp

答案 1 :(得分:6)

首先,这些都不是图书馆:

example.cpp tinyxml.h tinyxml.cpp tinystr.h tinystr.cpp

您需要找到tinyxml库。这个位置应该在包的文档中描述,但是让我们说它在/ foo(它不会)并且被称为libtinyxml.a(它可能不会)。你可以像这样链接它:

g++ example2.cpp -L/foo -ltinyxml

将/ foo添加到链接路径并在其上查找libtinyxml.a。或者您可以简单地提供库的完整路径:

g++ example2.cpp /foo/libtinyxml.a

答案 2 :(得分:1)

为什么不尝试了解Make? 它实际上非常简单。从第0列开始的每一行都是具有先决条件的目标。以制表符开头的行描述了构建目标的一个步骤。例如:

example2: example2.cpp example2.h
    g++ example2.cpp tinyxml.cpp tinystr.cpp -oexample2

或者,如果你有图书馆:

example2: example2.cpp example2.h
    g++ example2.cpp -Ltinyxml -oexample2

实际上,您不必指定您正在使用的编译器。也许你在这里使用一个,在另一个平台上使用另一个。所以你可以使用相应的make变量:

example2: example2.cpp example2.h
    $(CXX) example2.cpp -Ltinyxml -oexample2

就是这样。将其保存为名为“Makefile”的文件,并且为了编译项目,只需键入:

$ make

如果需要,它会编译它。 进一步阅读:

http://www.gnu.org/software/make/manual/html_node/index.html

答案 3 :(得分:0)

你应该编译g ++ main.cpp tinyxml.cpp tinystr.cpp tinyxmlparser.cpp tinyxmlerror.cpp

这些文件(main.cpp tinyxml.cpp tinystr.cpp tinyxmlparser.cpp tinyxmlerror.cpptinyxml.h tinystr.h)必须位于同一目录下你正在编译。

答案 4 :(得分:0)

实际上我昨天有这个问题。您可以尝试:g ++ * .cpp