C++ 使用TinyXML(链接库?在c+;+;)
+嗨。。。 我是一个新手。。。我不知道如何在C++中包括外部库。 这太难了 我想用TinyXML。 所以我做了这个: 例2.cppC++ 使用TinyXML(链接库?在c+;+;),c++,tinyxml,C++,Tinyxml,+嗨。。。 我是一个新手。。。我不知道如何在C++中包括外部库。 这太难了 我想用TinyXML。 所以我做了这个: 例2.cpp #include <iostream> #include "tinyxml.h" void write_app_settings_doc( ) { TiXmlDocument doc; TiXmlElement* msg; TiXmlDeclaration* decl = new TiXmlDeclaration(
#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;
}
<>我主要是java开发人员,C++中有很多问题要用到外部库。
我不知道什么时候链接。何时包括。当我必须做-i/myincludepath时/
这些文件位于同一文件夹中:
示例.cpp tinyxml.h tinyxml.cpp
tinystr.h tinystr.cpp
有人能帮我吗?试试这样的方法:
g++ example2.cpp tinyxml.cpp tinystr.cpp
首先,这些都不是图书馆:
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
example2: example2.cpp example2.h
g++ example2.cpp -Ltinyxml -oexample2
你为什么不试着学习一下Make呢? 其实很简单。从第0列开始的每一行都是具有先决条件的目标。以选项卡开头的一行描述了构建目标的一个步骤。例如:
example2: example2.cpp example2.h
g++ example2.cpp tinyxml.cpp tinystr.cpp -oexample2
或者,如果您有图书馆:
g++ example2.cpp /foo/libtinyxml.a
example2: example2.cpp example2.h
g++ example2.cpp -Ltinyxml -oexample2
实际上,您不必指定正在使用的编译器。也许你在这里用的是一个,在另一个平台上用的是另一个。因此,您可以使用适当的make变量:
example2: example2.cpp example2.h
$(CXX) example2.cpp -Ltinyxml -oexample2
就这样。将此文件另存为名为“Makefile”的文件,为了编译项目,只需键入:
$ make
如果需要,它会编译它。
进一步阅读:
您应该编译g++
main.cpp
tinyxml.cpp
tinystr.cpp
tinyxmlparser.cpp
tinyxmlerro.cpp
这些文件(
main.cpp
tinyxml.cpp
tinystr.cpp
tinyxmlparser.cpp
tinyxmlerro.cpp
和tinyxml.h
tinystr.h
)在编译时必须在一个目录下。事实上,我昨天遇到了这个问题。你可以试试看:g++*.cpp这很有效。。。我还忘了:tinyxmleror.cpp tinyxmlparser.cpp,所以解决方案是:g++example2.cpp tinyxml.cpp tinystr.cpp tinyxmleror.cpp tinyxmlparser.cppYes——在某个时候,您(可能)不想编译所有“微小”文件,然后使用ar
将它们放在一个名为“libtinyxml.a”的库中。然后在命令行上传递-ltinxml
。这有两个优点:它避免了在您使用tinyXML代码时重新编译它,并且它允许链接器只向您的可执行文件中添加必要的代码,而不是添加所有的代码,无论是否需要(尽管它可能会去掉不必要的部分,所以差别很小)。