Search code examples
cxmlgcccygwinlibxml2

C code using libxml2 giving compile time errors on cygwin on Win 7


I am trying to write a xml parser in C using the libxml2 on cygwin on my Win 7 machine. When i am trying to compile the code it is giving me compile time errors. Can someone help me out on this.

ad@ad-PC /cygdrive/c/Users/ad/Desktop
$ gcc xmlparser.c -I /cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/include/ -L /cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/lib/libxml2.lib -o pxml
In file included from /cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/include/libxml/parser.h:807,
             from xmlparser.c:8:
/cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/include/libxml/encoding.h:28:19: iconv.h: No such file or directory
In file included from /cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/include/libxml/parser.h:807,
             from xmlparser.c:8:
/cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/include/libxml/encoding.h:146: error: parse error before "iconv_t"
/cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/include/libxml/encoding.h:146: warning: no semicolon at end of struct or union
/cygdrive/c/Users/ad/Desktop/libxml2-2.7.8.win32/include/libxml/encoding.h:147: warning: data definition has no type or storage class
xmlparser.c: In function `main':
xmlparser.c:21: error: `filename' undeclared (first use in this function)
xmlparser.c:21: error: (Each undeclared identifier is reported only once  
xmlparser.c:21: error: for each function it appears in.)
xmlparser.c:29: error: parse error before ')' token
xmlparser.c: At top level:
xmlparser.c:34: error: parse error before string constant
xmlparser.c:34: error: conflicting types for 'printf'
xmlparser.c:34: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
xmlparser.c:34: error: conflicting types for 'printf'
xmlparser.c:34: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
xmlparser.c:34: warning: data definition has no type or storage class

The code ::

# include <stdio.h>
# include <stdlib.h>
# include <libxml/parser.h>

int main(int argc, char **argv)
{
    xmlDoc *document;
    xmlNode *root , *node , *first_child;

    if(argc < 2) 
    {
        printf("\n Error No filename specified \n");
        return 1;
    }

    filename = argv[1];

    document = xmlReadFile(filename,NULL,0);
    root = xmlDocGetRootElement(document);

    printf("\n Root is ::%s:: <%i> \n",root->name,root->type);
    first_child = root->children;

    for(node = first_child;node,node = node->next)
    {
        printf("\n Child is ::%s:: <%i> \n",node -> name, node->type);
    }

    printf("\n END \n");
    return 0;
    }

Solution

  • The error message says it unambiguously that filename is not declared and yet you're trying to use it here:

    filename = argv[1];
    

    There's also a wrong punctuator here:

    for(node = first_child;node,node = node->next)
    

    It has to be ; instead of ,.