日期:2014-05-16  浏览次数:21525 次

linux下用tinyxml来解析XML
根据网上的这个例子编译,却出现一些“undefined reference to”的错误;
错误如下:
8-3.cpp:(.text+0x81): undefined reference to `TiXmlDocument::TiXmlDocument(char const*)'
8-3.cpp:(.text+0x94): undefined reference to `TiXmlDocument::LoadFile(TiXmlEncoding)'
8-3.cpp:(.text+0x1a7): undefined reference to `TiXmlElement::GetText() const'
8-3.cpp:(.text+0x1f0): undefined reference to `TiXmlElement::Attribute(char const*) const'
/tmp/cceasJCM.o: In function `TiXmlNode::FirstChildElement()':
8-3.cpp:(.text._ZN9TiXmlNode17FirstChildElementEv[TiXmlNode::FirstChildElement()]+0xd): undefined reference to `TiXmlNode::FirstChildElement() const'
/tmp/cceasJCM.o: In function `TiXmlNode::FirstChild(char const*)':
8-3.cpp:(.text._ZN9TiXmlNode10FirstChildEPKc[TiXmlNode::FirstChild(char const*)]+0x14): undefined reference to `TiXmlNode::FirstChild(char const*) const'
/tmp/cceasJCM.o: In function `TiXmlNode::NextSibling(char const*)':
8-3.cpp:(.text._ZN9TiXmlNode11NextSiblingEPKc[TiXmlNode::NextSibling(char const*)]+0x14): undefined reference to `TiXmlNode::NextSibling(char const*) const'
/tmp/cceasJCM.o: In function `TiXmlString::quit()':
8-3.cpp:(.text._ZN11TiXmlString4quitEv[TiXmlString::quit()]+0xc): undefined reference to `TiXmlString::nullrep_'
/tmp/cceasJCM.o: In function `TiXmlDocument::~TiXmlDocument()':
8-3.cpp:(.text._ZN13TiXmlDocumentD1Ev[TiXmlDocument::~TiXmlDocument()]+0xb): undefined reference to `vtable for TiXmlDocument'
8-3.cpp:(.text._ZN13TiXmlDocumentD1Ev[TiXmlDocument::~TiXmlDocument()]+0x24): undefined reference to `TiXmlNode::~TiXmlNode()'
collect2: ld 返回 1

///////////////////////////////////////////////////////////////////////////
下面是要编译的测试程序
#include <iostream>
#include "tinyxml.h"

using namespace std;

int main(int,char *[])
{
 TiXmlDocument dom("D:/1.xml");
 bool bSuccess = dom.LoadFile();
 if (!bSuccess)
 {
  cout<<"打开失败!"<<endl;
  cout<<"错误的原因:"<<dom.ErrorDesc()<<endl;
 }
 TiXmlElement *pElement=dom.FirstChildElement();

 TiXmlNode* pRecord=pElement->FirstChild("Record");
 while (pRecord != 0)
 {
  // 得到结点里的内容
  TiXmlNode* pName=pRecord->FirstChild("Name");
  TiXmlElement* pNameEle=pName->ToElement();
  cout<<"结点名:"<<pNameEle->Value()<<endl;
  cout<<"内容:"<<pNameEle->GetText()<<endl;

  // 得到结点的属性
  cout<<" 结点的属性"<<pNameEle->Attribute("id")<<endl;

  // 通过自己来找与自己同层的兄弟
  pRecord=pRecord->NextSibling("Record");
 }
 getchar();
 return 0;


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hxzb7215191/archive/2006/12/08/1434960.aspx

------解决方案--------------------
你根本没有安装的tinyxml相关的库文件把!
参考这篇http://blog.csdn.net/chenxiaohua/archive/2009/06/16/4272171.aspx
------解决方案--------------------
也可能是安装时没装到linux默认的路径,g++找不到库文件
------解决方案--------------------
这种情况基本上可以说是找不到tinyxml的静态/动态库导致的。编译参数里面加上库路径。