如何在 c 程序中编译 expat xml 解析?以及在哪里安装外籍人士。将文件放入 cygwin 编译器的位置
how to compile an expat xml parsing in c Program? and where to install the expat. where to put the files in cygwin compiler
这是我第一次使用外部 headers,我对外籍人士一无所知。
Expat 官方网站对初学者没有用。
我需要使用 expat.h 在 C 中编译一个 XML 解析器代码。
我已经下载了 expat 文件并将路径包含在 header 文件中。
但在编译时出现错误。
在 windows 平台
#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;
}
这是代码,安装后expat.h所在的路径。
xml代码
`<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>`
请帮忙!
您似乎知道您必须执行 -lexpat
以便头文件实际上 link 到某些东西,但您忘记将 Expat 添加到 link 路径。添加此命令行选项:-L /path/to/your/expat/lib/directory
。对于 WinMain 错误,尝试添加 -Wl,-subsystem,console
。希望对您有所帮助!
Mingw 提供了 expat 库,所以在安装它自己的时候我们必须选择安装 expat 的选项。
gcc filename.c -llibexpat
足以编译无错代码。
不需要其他路径。
这是我第一次使用外部 headers,我对外籍人士一无所知。
Expat 官方网站对初学者没有用。
我需要使用 expat.h 在 C 中编译一个 XML 解析器代码。
我已经下载了 expat 文件并将路径包含在 header 文件中。
但在编译时出现错误。
在 windows 平台
#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;
}
这是代码,安装后expat.h所在的路径。
xml代码
`<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>`
请帮忙!
您似乎知道您必须执行 -lexpat
以便头文件实际上 link 到某些东西,但您忘记将 Expat 添加到 link 路径。添加此命令行选项:-L /path/to/your/expat/lib/directory
。对于 WinMain 错误,尝试添加 -Wl,-subsystem,console
。希望对您有所帮助!
Mingw 提供了 expat 库,所以在安装它自己的时候我们必须选择安装 expat 的选项。
gcc filename.c -llibexpat
足以编译无错代码。
不需要其他路径。