我已经用C++编写了一个程序来演示libxml2的用法。代码如下
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
void parseStory (xmlDocPtr doc, xmlNodePtr cur) {
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
printf("keyword: %s\n", key);
xmlFree(key);
}
cur = cur->next;
}
return;
}
static void parseDoc(char *docname) {
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile(docname);
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
parseStory (doc, cur);
}
cur = cur->next;
}
xmlFreeDoc(doc);
return;
}
int main(int argc, char **argv) {
char *docname;
if (argc <= 1) {
printf("Usage: %s docname\n", argv[0]);
return(0);
}
docname = argv[1];
parseDoc (docname);
return (1);
}我使用的是Ubuntu,我已经安装了libxml2-dev包,并使用
g++ Libxml2Example.cpp -I/usr/include/libxml2/libxml -lxml2 -o output但是我得到了以下错误
Build of configuration Debug for project Libxml2Example ****
make all
Building file: ../src/Libxml2Example.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Libxml2Example.d" - MT"src/Libxml2Example.d" -o"src/Libxml2Example.o" "../src/Libxml2Example.cpp"
../src/Libxml2Example.cpp:12:30: fatal error: libxml/xmlmemory.h: No such file or directory
compilation terminated.
make: *** [src/Libxml2Example.o] Error 1这个错误怎么解决?请帮帮我。
发布于 2014-12-29 16:42:20
目录libxml必须存在于由以下任一环境变量指定的现有隐式搜索包含路径中:
CPATH
CPLUS_INCLUDE_PATH
GCC_INCLUDE_DIR或其路径必须由`-I显式指定。
我敢打赌,在编译之后,您将在链接、查找libxml.a文件或设置环境变量LIBRARY_PATH时遇到类似的问题。
通常,为了使您的项目及其构建文件可以移植到其他安装,而不必设置构建环境,应该避免使用环境变量方法。
发布于 2014-12-29 16:49:13
首先,您需要安装libxml2的开发包
sudo apt-get install libxml2-dev这将安装库include文件。
在项目中,您需要使用-I/usr/include/libxml2/进行编译
即
g++ -O0 -g3 -Wall ../src/Libxml2Example.cpp -I/usr/include/libxml2/ -lxml2 -L/usr/lib/x86_64-linux-gnu/在x86_64 ubuntu系统中,这些库安装在/usr/lib/x86_64-linux-gnu/中。
https://stackoverflow.com/questions/27685646
复制相似问题