libxml2编译错误问题

安装环境略;

测试源码:

#include<stdio.h>
#include<stdlib.h>
#include<string.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 parse successfull.\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);
}

出现的错误:gcc test.c - o test

xml.c:4:29: error: libxml/xmlmemory.h: No such file or directory
xml.c:5:26: error: libxml/parser.h: No such file or directory
xml.c:8: error: expected ‘)’ before ‘doc’
xml.c:14:44: warning: missing terminating " character
xml.c:14: error: missing terminating " character
xml.c: In function ‘parseDoc’:
xml.c:28: error: ‘xmlDocPtr’ undeclared (first use in this function)
xml.c:28: error: (Each undeclared identifier is reported only once
xml.c:28: error: for each function it appears in.)
xml.c:28: error: expected ‘;’ before ‘doc’
xml.c:29: error: ‘xmlNodePtr’ undeclared (first use in this function)
xml.c:29: error: expected ‘;’ before ‘cur’
xml.c:30: error: ‘doc’ undeclared (first use in this function)
xml.c:36: error: ‘cur’ undeclared (first use in this function)
xml.c:43: error: expected ‘)’ before ‘xmlChar’
xml.c:52: error: ‘sur’ undeclared (first use in this function)
xml.c:52: error: expected ‘)’ before ‘xmlChar’

解决办法:

用命令:gcc xml.c -o xml -I/usr/include/libxml2/ -lxml2可解决

解释:

这个 -I 和 -l 的参数解释是这样的:

-Idir 当用#include <file> 链接文件的时候,gcc/g++会先在当前目录查找你所制定的头文件,如果没有找到,他回到缺省的头文件目录找,如果使用-I制定了目录,他会先在你所制定的目录查找,然后再按常规的顺序去找。

-llibrary 制定编译的时候使用的库。

例子用法: gcc -lcurses hello.c   /* 使用ncurses库编译程序 */

 所以“gcc -o xml-build xml-build.c -I/usr/include/libxml2/ -lxml2”命令的意思就是“在指定目录/usr/include/libxml2里寻找头文件,编译时使用的库是xml2函数库”

ps:

这个命令也可以成功编译:gcc `xml2-config --cflags --libs` -o xml-create xml-create.c

posted @ 2014-01-01 15:42  刘俊鹏123  阅读(1084)  评论(0编辑  收藏  举报
重生之大文豪