在 C++ 中使用 libXml2与 Netbeans

发布于 2024-12-04 20:39:23 字数 2951 浏览 1 评论 0原文

我在获取 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':
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:26: undefined reference to
xmlTextReaderConstName'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:30: undefined reference to xmlTextReaderConstValue'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to
xmlTextReaderHasValue'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderIsEmptyElement'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to
xmlTextReaderNodeType'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:37: undefined reference to xmlTextReaderDepth'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:41: undefined reference to
xmlStrlen'
build/Debug/MinGW-Windows/XmlParser.o: In function ZN9XmlParser10streamFileEPKc':
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:51: undefined reference to
xmlReaderForFile'
C:\Users...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:53: undefined reference to xmlTextReaderRead'
C:\Users\...\Documents\NetBeansProjects\XmlBallotParser/XmlParser.cpp:56: undefined reference to
xmlTextReaderRead'
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

允世 2024-12-11 20:39:23

-lxml2 (gcc) 或 libxml2.lib (Visual Studio) 添加到链接器中。

Add -lxml2 (gcc) or libxml2.lib (visual studio) to your linker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文