关于libxml2库的详细信息的查看方法请查看: http://blog.csdn.net/wangkai_123456/article/details/25710971 libxml2库的安装方法: sudo apt-get install libxml2 sudo apt-get install libxml2-dev 利用以上命令安装完软件包后可以利用以下命令来查看软件
关于libxml2库的详细信息的查看方法请查看:http://blog.csdn.net/wangkai_123456/article/details/25710971
libxml2库的安装方法:
sudo apt-get install libxml2
sudo apt-get install libxml2-dev
利用以上命令安装完软件包后可以利用以下命令来查看软件包的状态(确定是否安装成功)
dpkg -s libxml2-dev
也可以用如下命令(得到的结果是一样的):
dpkg-query -s libxml2-dev
软件安装好后,默认情况下,libxml2对应的头文件均放置在/usr/include/libxml2/libxml目录下,这一信息可利用如下两个命令查看(任一命令都能查看到相关信息)
dpkg -l libxml2-dev
xml2-config --cflags
libxml2的库文件放置在/usr/lib/i386-linux-gnu目录下,这一信息可利用如下两个命令查看(任一命令都能查看到相关信息)
dpkg -l libxml2-dev
xml2-config --libs
对应libxml2有一个工具,名字为xml2-config,xml2-config所在的目录为/usr/bin,其实这是一个shell脚本,关于xml2-config的详细信息可以通过如下命令来查看
man xml2-config
安装后libxml2库后,就可以开始进行基于libxml2库的开发了。现有以下一段程序,程序文件名为createxmlfile.c,是基于libxml2库开发的。代码如下
/**********************************created: 2014/05/12filename: createxmlfile.cauther: wang kaidepend: libxml2.libpurpose: 创建一个xml文件**********************************/#include#include#includeint main(int argc, char **argv){ //define document pointer xmldocptr doc = xmlnewdoc(bad_cast1.0); //define node pointer xmlnodeptr root_node = xmlnewnode(null,bad_castroot); //set the root element of the document xmldocsetrootelement(doc,root_node); //create child nodes directly in the root node xmlnewtextchild(root_node,null,bad_castnewnode1,bad_castnewnode1 content); xmlnewtextchild(root_node,null,bad_castnewnode2,bad_castnewnode2 content); //create a new node xmlnodeptr node = xmlnewnode(null,bad_castnode2); //create a new text node xmlnodeptr content = xmlnewtext(bad_castnode content); //add a new node to parent xmladdchild(root_node,node); xmladdchild(node,content); //create a new property carried by a node xmlnewprop(node,bad_castattribute,bad_castyes); //create a son and grandson node element node = xmlnewnode(null,bad_castson); xmladdchild(root_node,node); xmlnodeptr grandson = xmlnewnode(null,bad_castgrandson); xmladdchild(node,grandson); xmladdchild(grandson,xmlnewtext(bad_castthis is a grandson node)); //dump an xml document to a file int nrel = xmlsavefile(createdxml.xml,doc); if(nrel != -1) printf(一个xml文档被创建,写入 %d 个字节\n,nrel); //free up all the structures used by a document,tree included xmlfreedoc(doc); //printf(hello world!\n); return 0;}
对这个程序进行编译时,可用以下命令gcc -i/usr/include/libxml2 createxmlfile.c -o createxmlfile-l /usr/lib/i386-linux-gnu -lxml2
其中,-i参数是为了指定gcc编译器查找头文件的路径,-l参数是为了指定libxml2库文件所在的路径,最后的
-lxml2指定具体的库文件。(-lxml2一定要放在命令的最后位置,不然会出现找不到链接库的错误,如下图所示)
具体为什么一定要把-lxml2放在最后的位置,本人目前还没弄明白,有待进一步研究
编译命令也可以写成如下形式:
gcc `xml2-config --cflags` -l /usr/lib/i386-linux-gnu createxmlfile.c -o createxmlfile -lxml2
或
gcc `xml2-config --cflags` createxmlfile.c -o createxmlfile-l /usr/lib/i386-linux-gnu -lxml2
或
gcc createxmlfile.c -o createxmlfile `xml2-config --cflags --libs`
形式虽然不一样,其实命令的实际内容是一样的。因为命令xml2-config --cflags的执行结果为
-i/usr/include/libxml2 (指明include头文件所在的目录)
命令xml2-config --libs的执行结果为
-l/usr/lib/i386-linux-gnu -lxml2 (指明libxml2库文件所在的目录以及具体的库文件)
(不管写成何种形式,只要保证 -lxml2 在编译命令的最后位置即可)