c - GCC 无法识别 libxml2 函数

标签 c parsing gcc libxml2

当尝试使用 libxml2 编译简单的解析程序时,GCC 返回此错误。

/tmp/ccuq3Hc1.o: In function `main':
xmltesting.c:(.text+0x31): undefined reference to `htmlCreatePushParserCtxt'
xmltesting.c:(.text+0x50): undefined reference to `htmlCtxtReadFile'
xmltesting.c:(.text+0x64): undefined reference to `xmlDocGetRootElement'
collect2: error: ld returned 1 exit status

代码:

#include <stdio.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/parser.h>

int main()
{

    htmlDocPtr doc;
    htmlNodePtr org_node;
    htmlNodePtr curnt_node = NULL;

    htmlParserCtxtPtr parser = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, 0);

    doc = htmlCtxtReadFile( parser, "html.txt", NULL, 0);
    org_node = xmlDocGetRootElement( parser->myDoc );

    for ( curnt_node = org_node; curnt_node; curnt_node =  curnt_node->next ) {

        if ( curnt_node->type == XML_TEXT_NODE ) {

            printf ("%s", curnt_node->content );

        }

    }

}

看起来结构体读得很好,但是函数却找不到?

或者我的代码有问题吗?

最佳答案

您可能只需在 gcc 构建行的末尾添加 -lxml2 即可。

<小时/>

正如 Dietricg Epp 在评论中指出的那样,如果您的库有正确的 .pc 文件,pkg-config 是获取必要标志的首选方法。您可以获得编译和链接标志。

$ pkg-config libxml-2.0 --cflags --libs
-I/usr/include/libxml2  -lxml2

关于c - GCC 无法识别 libxml2 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21589202/

相关文章:

c - 如何使用 GNU 链接器而不是 Darwin 链接器?

c - 如果一个单词截断一个 int 会发生什么?

c++ - c 在 C++ 中转换,编译时还是运行时?

c++ - 为 std::vector<user-defined> 赋值

PHP 解析包含

linux - 如何找到printf(库函数)的堆栈大小?

c - 当指针指向使用 malloc() 获得的内存位置时,编译器如何处理 CONST 限定符?

parsing - 可选尾随逗号的缺点是什么?

c# - 一般的 TryParse 可空类型

c++ - 如何计算 ELF 文件中的静态初始值设定项?