【问题标题】:netfilter queue undefined reference to `nfq_open'netfilter 队列未定义对“nfq_open”的引用
【发布时间】:2014-12-17 06:43:25
【问题描述】:

我已经写了这段代码:

#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

【问题讨论】:

  • netfilter 标签用于 netfilter_queue(顺便说一下这个标签丢失了),我把它放在以防有人遇到同样的问题,但我认为这是一个更普遍的问题
  • 请出示您的编译声明。
  • 感谢您指出这一点(我编辑了我的帖子以包含它)
  • 你试过gcc -o test test1.c -lnetfilter_queue吗?
  • 不严重,非常感谢(我白白浪费了 3 个小时)

标签: c linux ubuntu gcc netfilter


【解决方案1】:

好吧,来自gcc manual page,用于-llibrary 链接选项

在命令的哪个位置编写此选项会有所不同;链接器按照指定的顺序搜索和处理库和目标文件。因此,“foo.o -lz bar.o”在文件 foo.o 之后但在 bar.o 之前搜索库“z”。如果 bar.o 引用了 ‘z’ 中的函数,这些函数可能不会被加载。

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

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

 gcc -o test test1.c -lnetfilter_queue

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2019-07-31
    • 2011-03-27
    • 2022-01-22
    相关资源
    最近更新 更多