Search code examples
cxmlparsingexpat-parser

how to compile an expat xml parsing in c Program? and where to install the expat. where to put the files in cygwin compiler


This is my first time using external headers and i have no idea about expat. Expat official website is not useful for a beginner. i need to compile a XML parser code in C using expat.h. i have downloaded expat files and included the path in header file. but while compiling i get error. in windows platform enter image description here

enter image description here

    #include "C:\Program Files (x86)\Expat 2.2.7\Source\lib\expat.h"
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>

     int parse_xml(char *buff)
     {
    FILE *fp;
    fp = fopen("config.xml", "r");
    if(fp == NULL)
    {
        printf("Failed to open file\n");
        return 1;
    }

   /* Obtain the file size. */
    fseek (fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    rewind(fp);

    XML_Parser parser = XML_ParserCreate(NULL);
    int done;
    memset(buff, 0, sizeof(buff));

    do
    {
        size_t len = fread(buff, 1, file_size, fp);
        done = len < sizeof(buff);

        if(XML_Parse(parser, buff, len, done) == XML_STATUS_ERROR)
        {
            printf("%s at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)),
                                      XML_GetCurrentLineNumber(parser));
            return 1;
        }
    }
    while(!done);

    fclose(fp);
    XML_ParserFree(parser);

    return 0;
}

this is the code and the path is where the expat.h is located after installation.

xml code

`<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology
      society in England, the young survivors lay the
      foundation for a new society.</description>
   </book>
</catalog>`

Please help!


Solution

  • You seem to know that you have to do -lexpat so that the header files actually link to something, but you forgot to add Expat to the link path. Add this command line option: -L /path/to/your/expat/lib/directory. For the WinMain error, try adding -Wl,-subsystem,console. Hope it helps!