当尝试使用 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/