gpt4 book ai didi

c - netfilter 队列 undefined reference `nfq_open'

转载 作者:可可西里 更新时间:2023-11-01 11:49:04 31 4
gpt4 key购买 nike

我已经写了这段代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
int main(int argc, char **argv)
{
struct nfq_handle *h;
printf("opening library handle\n");
h = nfq_open();
nfq_close(h);
exit(0);
}

当我尝试编译时它说:

/tmp/ccEv9MYS.o: In function `main':
test1.c:(.text+0x1a): undefined reference to `nfq_open'
test1.c:(.text+0x2a): undefined reference to `nfq_close'
collect2: ld returned 1 exit status

我尝试检查该库是否被 gcc 找到并且它是(当我修改 libnetfilter_queue 的包含时出现错误),我重新编译了该库并确认我正在调用的函数在其中。

如果你有任何线索,谢谢你的帮助

我用这个编译:

gcc -o test test1.c

我也试过:

gcc -o test -lnetfilter_queue test1.c
gcc -o test -L/usr/local/lib test1.c

最佳答案

嗯,来自 gcc manual page , 对于 -llibrary 链接选项

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

也就是说,链接器从左到右工作,因此需要将依赖项放在左侧。

你需要把你的编译语句改成

 gcc -o test test1.c -lnetfilter_queue

关于c - netfilter 队列 undefined reference `nfq_open',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519730/

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