在C中编译Xml解析器时出现线程错误?

时间:2012-05-30 06:15:16

标签: c mxml

[root@localhost mxml-2.7]# gcc -o xml XmlParser1.c -lmxml
XmlParser1.c: In function ‘main’:
XmlParser1.c:63: warning: assignment discards qualifiers from pointer target type
/usr/local/lib/libmxml.so: undefined reference to `pthread_key_create'
/usr/local/lib/libmxml.so: undefined reference to `pthread_once'
/usr/local/lib/libmxml.so: undefined reference to `pthread_getspecific'
/usr/local/lib/libmxml.so: undefined reference to `pthread_key_delete'
/usr/local/lib/libmxml.so: undefined reference to `pthread_setspecific'
collect2: ld returned 1 exit status

编译XmlParser1.c时发生以下错误。 XmlParser1.c:

#include <stdio.h>
#include "mxml.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define MAX_SIZE 25

static mxml_node_t *xml; 
static mxml_node_t *str;
static mxml_node_t *DataSet;

static mxml_node_t *Table;
static mxml_node_t *IsAuthenticated;
static mxml_node_t *AuthenticationDate;
static mxml_node_t *Response;

int main()
{
    int fd = 0;
    char *Result=NULL;
    const char *NewResult=NULL;
    mxml_node_t *tree;
    mxml_node_t *data;
    const char *type = NULL;

    FILE *fp = fopen("/home/suneet/mxml-2.7/Sample/main.xml", "r")  ;
    if (fp == NULL)
    {
        fclose(fp);
    }
    else
    {
        fseek (fp , 0, SEEK_END);
        long settings_size = ftell (fp);
        rewind (fp);

        if (settings_size > 0)
        {
            tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);
            fclose(fp);

            printf("step 1\n");
            data = mxmlFindElement(Table, tree, "diffgr:id", NULL, NULL, MXML_DESCEND);
            Result = mxmlElementGetAttr(data,"diffgr:id");
            printf("diffgr:id:%s\n",(Result == NULL)?"NULL":Result);
            mxmlDelete(data);
            mxmlDelete(tree);   
        }
    }

    return 0;
}

当我试图采用http://minixml.org/中给出的步骤时;因此解析xml文件存在某些错误“动态库libxml.so的线程未定义错误”。

请指导我,以便我可以成功解析xml文件。

2 个答案:

答案 0 :(得分:2)

您需要使用-pthread选项链接到pthread库。

gcc -o xml XmlParser1.c -lmxml -pthread

答案 1 :(得分:1)

您需要在代码中包含pthread。 在文件的开头添加此行

#include<pthread.h>