You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译libnftnl示例代码出现链接错误,如何解决?

问题

我需要编译nftables归档中的libnftnl示例源码,操作步骤如下:

  • 执行命令sudo apt-get source libnftnl11获取源码包
  • 进入libnftnl-1.2.1/examples/目录,该目录下有多个包含main函数的小型示例源码文件
  • 尝试使用gcc nft-chain-add.c -o main -lnftables编译nft-chain-add.c,却出现以下链接错误:
gcc nft-chain-add.c -o main -lnftables 
/usr/bin/ld: /tmp/cc8sxDLv.o: in function `chain_add_parse':
nft-chain-add.c:(.text+0x133): undefined reference to `nftnl_chain_alloc'
/usr/bin/ld: nft-chain-add.c:(.text+0x170): undefined reference to `nftnl_chain_set_str'
/usr/bin/ld: nft-chain-add.c:(.text+0x18c): undefined reference to `nftnl_chain_set_str'
/usr/bin/ld: nft-chain-add.c:(.text+0x1a6): undefined reference to `nftnl_chain_set_u32'
/usr/bin/ld: nft-chain-add.c:(.text+0x1cc): undefined reference to `nftnl_chain_set_u32'
/usr/bin/ld: /tmp/cc8sxDLv.o: in function `main':
nft-chain-add.c:(.text+0x454): undefined reference to `mnl_nlmsg_batch_start'
/usr/bin/ld: nft-chain-add.c:(.text+0x470): undefined reference to `mnl_nlmsg_batch_current'
/usr/bin/ld: nft-chain-add.c:(.text+0x47b): undefined reference to `nftnl_batch_begin'
/usr/bin/ld: nft-chain-add.c:(.text+0x487): undefined reference to `mnl_nlmsg_batch_next'
/usr/bin/ld: nft-chain-add.c:(.text+0x4ac): undefined reference to `mnl_nlmsg_batch_current'
/usr/bin/ld: nft-chain-add.c:(.text+0x4c4): undefined reference to `nftnl_nlmsg_build_hdr'
/usr/bin/ld: nft-chain-add.c:(.text+0x4db): undefined reference to `nftnl_chain_nlmsg_build_payload'
/usr/bin/ld: nft-chain-add.c:(.text+0x4e7): undefined reference to `nftnl_chain_free'
/usr/bin/ld: nft-chain-add.c:(.text+0x4f3): undefined reference to `mnl_nlmsg_batch_next'
/usr/bin/ld: nft-chain-add.c:(.text+0x50b): undefined reference to `mnl_nlmsg_batch_current'
/usr/bin/ld: nft-chain-add.c:(.text+0x516): undefined reference to `nftnl_batch_end'
/usr/bin/ld: nft-chain-add.c:(.text+0x522): undefined reference to `mnl_nlmsg_batch_next'
/usr/bin/ld: nft-chain-add.c:(.text+0x52c): undefined reference to `mnl_socket_open'
/usr/bin/ld: nft-chain-add.c:(.text+0x566): undefined reference to `mnl_socket_bind'
/usr/bin/ld: nft-chain-add.c:(.text+0x58f): undefined reference to `mnl_socket_get_portid'
/usr/bin/ld: nft-chain-add.c:(.text+0x59e): undefined reference to `mnl_nlmsg_batch_size'
/usr/bin/ld: nft-chain-add.c:(.text+0x5ad): undefined reference to `mnl_nlmsg_batch_head'
/usr/bin/ld: nft-chain-add.c:(.text+0x5c2): undefined reference to `mnl_socket_sendto'
/usr/bin/ld: nft-chain-add.c:(.text+0x5ec): undefined reference to `mnl_nlmsg_batch_stop'
/usr/bin/ld: nft-chain-add.c:(.text+0x602): undefined reference to `mnl_socket_recvfrom'
/usr/bin/ld: nft-chain-add.c:(.text+0x62b): undefined reference to `mnl_cb_run'
/usr/bin/ld: nft-chain-add.c:(.text+0x64a): undefined reference to `mnl_socket_recvfrom'
/usr/bin/ld: nft-chain-add.c:(.text+0x681): undefined reference to `mnl_socket_close'
collect2: error: ld returned 1 exit status

解决方法

1. 链接正确的库文件

你当前用的-lnftables是nftables主程序的库,而示例依赖的是libnftnl和libmnl两个库,编译命令需要改成:

gcc nft-chain-add.c -o main -lnftnl -lmnl
  • -lnftnl:对应nftnl_*开头的函数(比如nftnl_chain_alloc)
  • -lmnl:对应mnl_*开头的函数(比如mnl_nlmsg_batch_start)

2. 确保依赖库已安装开发包

如果执行上述命令仍报错,说明缺少库的开发文件,先安装:

sudo apt-get install libnftnl-dev libmnl-dev

3. 直接使用源码自带的Makefile编译(推荐)

libnftnl源码包的examples目录下已有现成的Makefile,直接执行make就能自动编译所有示例,无需手动编写gcc命令:

cd libnftnl-1.2.1/examples/
make

编译完成后,当前目录会生成对应示例的可执行文件,直接运行即可。


内容的提问来源于stack exchange,提问作者mah454

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 23:34:55