在 C++ 中使用 libXml2与 Netbeans
我在获取 C++ 代码在 Netbeans 中正确编译时遇到问题,特别是处理 libXml2 的代码。我下载了 libXml2,将其放入 include 文件夹中,我知道代码本身可以正常编译,无需任何 libxml2 引用,但是,当我添加以下函数时:
void XmlParser::processNode(xmlTextReaderPtr reader){
const xmlChar *name, *value;
name = xmlTextReaderConstName(reader);
if (name == NULL)
name = BAD_CAST "--";
value = xmlTextReaderConstValue(reader);
printf("%d %d %s %d %d",
xmlTextReaderDepth(reader),
xmlTextReaderNodeType(reader),
name,
xmlTextReaderIsEmptyElement(reader),
xmlTextReaderHasValue(reader));
if (value == NULL)
printf("\n");
else {
if (xmlStrlen(value) > 40)
printf(" %.40s...\n", value);
else
printf(" %s\n", value);
}
}
void XmlParser::streamFile(const char *filename) {
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
//processNode(reader);
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
if (ret != 0) {
fprintf(stderr, "%s : failed to parse\n", filename);
}
} else {
fprintf(stderr, "Unable to open %s\n", filename);
}
}
单击构建时得到以下结果:
build/Debug/MinGW -Windows/XmlParser.o:在函数 ZN9XmlParser11processNodeEP14_xmlTextReader' 中: C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:26:未定义引用
xmlTextReaderConstName' C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:30: 对 xmlTextReaderConstValue' 的未定义引用 C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: 未定义引用
xmlTextReaderHasValue' C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37:对 xmlTextReaderIsEmptyElement' 的未定义引用 C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: 对 xmlTextReaderNodeType' 的未定义引用 C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: 对
xmlTextReaderDepth' 的未定义引用 C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:41:未定义对 xmlStrlen 的引用' build/Debug/MinGW-Windows/XmlParser.o:在函数
ZN9XmlParser10streamFileEPKc' 中: C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:51:未定义引用
xmlReaderForFile' C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:53:对 xmlTextReaderRead' 的未定义引用 C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:56: 未定义引用
xmlTextReaderRead' C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:58: undefined reference to `xmlFreeTextReader'
这表明我在使用 libXml2 编译程序时遇到了某种问题。我看到这里有人有同样的错误,并且答案解决这个问题的方法是正确设置编译程序的参数,但是,我不知道如何在 NetBeans/Windows 中执行此操作。我认为我需要的是关于如何真正正确地论证论点的细节。任何帮助将不胜感激。谢谢。
I'm having a problem getting C++ code to compile properly in Netbeans, specifically code that deals with libXml2. I downloaded libXml2, put it in the include folder and I know that the code itself compiles fine without any libxml2 references in it, however, when I add the following functions:
void XmlParser::processNode(xmlTextReaderPtr reader){
const xmlChar *name, *value;
name = xmlTextReaderConstName(reader);
if (name == NULL)
name = BAD_CAST "--";
value = xmlTextReaderConstValue(reader);
printf("%d %d %s %d %d",
xmlTextReaderDepth(reader),
xmlTextReaderNodeType(reader),
name,
xmlTextReaderIsEmptyElement(reader),
xmlTextReaderHasValue(reader));
if (value == NULL)
printf("\n");
else {
if (xmlStrlen(value) > 40)
printf(" %.40s...\n", value);
else
printf(" %s\n", value);
}
}
void XmlParser::streamFile(const char *filename) {
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
//processNode(reader);
ret = xmlTextReaderRead(reader);
}
xmlFreeTextReader(reader);
if (ret != 0) {
fprintf(stderr, "%s : failed to parse\n", filename);
}
} else {
fprintf(stderr, "Unable to open %s\n", filename);
}
}
I get the following result when I click build:
build/Debug/MinGW-Windows/XmlParser.o: In function ZN9XmlParser11processNodeEP14_xmlTextReader':
xmlTextReaderConstName'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:26: undefined reference to
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:30: undefined reference to xmlTextReaderConstValue'
xmlTextReaderHasValue'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderIsEmptyElement'
xmlTextReaderNodeType'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderDepth'
xmlStrlen'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:41: undefined reference to
build/Debug/MinGW-Windows/XmlParser.o: In function ZN9XmlParser10streamFileEPKc':
xmlReaderForFile'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:51: undefined reference to
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:53: undefined reference to xmlTextReaderRead'
xmlTextReaderRead'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:56: undefined reference to
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:58: undefined reference to `xmlFreeTextReader'
Which suggests that I have some sort of an issue compiling the program using libXml2. I saw that someone right here had the same error, and that the answer to this problem is correctly setting up the argument for compiling the program, however, I cannot figure out how to do this in NetBeans/Windows. I think what I need is details on how to actually get the arguments right. Any help would be appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
-lxml2
(gcc) 或libxml2.lib
(Visual Studio) 添加到链接器中。Add
-lxml2
(gcc) orlibxml2.lib
(visual studio) to your linker.