这是我第一次使用外部标头,我不了解外籍人士。
Expat官方网站对初学者没有帮助。
我需要使用expat.h在C中编译XML解析器代码。
我已经下载了expat文件,并将路径包含在头文件中。
但是在编译时出现错误。
在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
以便头文件实际链接到某些内容,但是您忘记将Expat添加到链接路径。添加以下命令行选项:-L /path/to/your/expat/lib/directory
。对于WinMain错误,请尝试添加-Wl,-subsystem,console
。希望能帮助到你!
关于c - 如何在C程序中编译expat xml解析?以及在哪里安装外籍人士。将文件放在cygwin编译器中的位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57375894/