使用 TinyXML(在 c++ 中链接库)
+嗨... 我是一个新手...我不知道如何在 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++ 示例2.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.cpptinyxml.htinyxml.cpp tinystr.htinystr.cpp
有人可以帮助我吗?
+Hi...
i am a newbie ... and i don't know how to include external libraries in c++.
This is sooo hard.
I want to use TinyXML.
so i made this :
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;
}
just an example file.
So ..when i compile this with :
g++ example2.cpp
i get following error message:
/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
I am mostly a java developer and i have a lot of problems in c++ to use external libraries.
I dont know when to link . when to include. when i have to make -I/myincludepath/
these files are in the same folder :
example.cpp tinyxml.h tinyxml.cpp
tinystr.h tinystr.cpp
Can somebody help me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,这些都不是库:
您需要找到tinyxml 库。这个位置应该在包的文档中描述,但假设它在 /foo 中(它不会)并且被称为 libtinyxml.a (它可能不会)。您可以像这样链接它:
它将 /foo 添加到链接路径并在其上查找 libtinyxml.a 。或者您可以简单地提供库的完整路径:
Firstly, none of these is a library:
You need to locate the tinyxml library. This location should be described in the package's documentation, but lets say it's in /foo (it won't be) and is called libtinyxml.a (it probably wont be). You could link it like this:
which adds /foo to the link path and looks for libtinyxml.a on it. Or you can simply provide the complete path to the library:
尝试类似的方法:
Try something like:
你为什么不尝试学习Make呢?
其实很简单。从第 0 列开始的每一行都是一个目标及其先决条件。以选项卡开头的一行描述了构建目标的一个步骤。例如:
或者,如果您有库:
实际上,您不必指定您正在使用的编译器。也许您在这里使用一个,而在另一个平台上使用另一个不同的。所以你可以使用适当的 make 变量:
就是这样。将其保存为名为“Makefile”的文件,并且为了编译您的项目,只需键入:
如果需要,它将编译它。
进一步阅读:
http://www.gnu.org/software/make /manual/html_node/index.html
Why don't you try to learn about Make?
It is actually very simple. Each line beginning at column 0 is an objective with its prerequisites. A line beginning with a tab is describes one step to build the objective. For example:
Or, if you have the library:
Actually, you don't have to specify the compiler you're using. Maybe you're using one here, and another different one in another platform. So you can use the appropriate make's variable:
That's it. Save this as a file with name "Makefile" and, in order to compile your project, just type:
And it will compile it if needed.
Further reading:
http://www.gnu.org/software/make/manual/html_node/index.html
您应该编译 g++
main.cpp
tinyxml.cpp
tinystr.cpp
tinyxmlparser.cpp
tinyxmlerror.cpp
和这些文件(
main.cpp
tinyxml.cpp
tinystr.cpp
tinyxmlparser.cpp
tinyxmlerror.cpp
和tinyxml.h
tinystr.h
) 在编译时必须位于同一个目录下。You should compile g++
main.cpp
tinyxml.cpp
tinystr.cpp
tinyxmlparser.cpp
tinyxmlerror.cpp
and these files(
main.cpp
tinyxml.cpp
tinystr.cpp
tinyxmlparser.cpp
tinyxmlerror.cpp
ANDtinyxml.h
tinystr.h
) must be under one directory while you are compiling.其实我昨天也遇到了这个问题。你可以尝试:g++ *.cpp
Actually I had this problem yesterday.You can try :g++ *.cpp