gpt4 book ai didi

c - 链接 libxml2 时出错

转载 作者:太空狗 更新时间:2023-10-29 15:15:14 26 4
gpt4 key购买 nike

我有一个简单的 libxml2 示例,但它返回以下错误:

$ gcc -Wall -lxml2 -I/usr/include/libxml2 -o ex1 ex1.c
/tmp/cc6OKSKJ.o: In function `main':
ex1.c:(.text+0x60): undefined reference to `xmlReadFile'
ex1.c:(.text+0x70): undefined reference to `xmlDocGetRootElement'
collect2: ld returned 1 exit status
$ xml2-config --libs
-lxml2
$ xml2-config --cflags
-I/usr/include/libxml2

我在 Lubuntu 11.10 x86_64 上,我有我需要的所有包(我想):libxml2、libxml2-dev、libxml2-dbg...这是示例代码:

// gcc -Wall -lxml2 -I/usr/include/libxml2 -o ex1 ex1.c

#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>

int main(int argc, char **argv)
{
xmlDoc *document;
xmlNode *root, *first_child, *node;
char *filename;

if (argc < 2)
{
fprintf(stderr, "Usage: %s filename.xml\n", argv[0]);
return 1;
}
filename = argv[1];

document = xmlReadFile(filename, NULL, 0);
root = xmlDocGetRootElement(document);
fprintf(stdout, "Root is <%s> (%i)\n", root->name, root->type);
first_child = root->children;

for (node = first_child; node; node = node->next)
{
fprintf(stdout, "\t Child is <%s> (%i)\n", node->name, node->type);
}
fprintf(stdout, "...\n");
return 0;
}

最佳答案

您的链接行不正确。尝试

gcc -Wall -I/usr/include/libxml2 -o ex1 ex1.c -lxml2

阅读this了解为什么命令行上源和库的顺序很重要。

关于c - 链接 libxml2 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7815791/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com