clinuxgccnftables

Compile nftables examples


how to compile one example source code of nftables archive ?

> sudo apt-get source libnftnl11
> cd libnftnl-1.2.1/examples/
> ls -l 
Makefile.am      nft-chain-get.c           nft-ct-expectation-get.c  nft-ct-timeout-add.c  nft-flowtable-add.c  nft-obj-add.c   nft-rule-ct-expectation-add.c  nft-rule-get.c     nft-set-elem-add.c  nft-table-add.c
Makefile.in      nft-compat-get.c          nft-ct-helper-add.c       nft-ct-timeout-del.c  nft-flowtable-del.c  nft-obj-del.c   nft-rule-ct-helper-add.c       nft-ruleset-get.c  nft-set-elem-del.c  nft-table-del.c
nft-chain-add.c  nft-ct-expectation-add.c  nft-ct-helper-del.c       nft-ct-timeout-get.c  nft-flowtable-get.c  nft-obj-get.c   nft-rule-ct-timeout-add.c      nft-set-add.c      nft-set-elem-get.c  nft-table-get.c
nft-chain-del.c  nft-ct-expectation-del.c  nft-ct-helper-get.c       nft-events.c          nft-map-add.c        nft-rule-add.c  nft-rule-del.c                 nft-set-del.c      nft-set-get.c       nft-table-upd.c

all source codes is too small and contain main function.
for example i try to compile 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

How to fix this problem ?


Solution

  • I manually compiled source code.
    For example :

    gcc nft-chain-add.c -o main -lnftnl -lmnl