clinuxgccld

Error with libnftnl on Ubuntu 20.04. /usr/bin/ld: cannot find -lnftnl


I try to build a C program that uses the libnftnl library, but I get this error:

/usr/bin/ld: cannot find -lnftnl

Here is the way that I compile my code:

LIBS = -L/usr/lib/x86_64-linux-gnu/libmnl.so -L/usr/lib/x86_64-linux-gnu/libnftnl.so  -lmnl -lnftnl
CFLAGS = -static -s
prog:
    gcc -o prog prog.c $(LIBS) $(CFLAGS)

I install:

libnftnl-dev - Development files for libnftnl
libnftnl11 - Netfilter nftables userspace API library

But I still get the same error. What am I doing wrong?


Solution

  • You are specifying -static among your compile flags, but you seem to be expecting to link shared libraries. That won't work -- the libraries you need for static linking are of a different kind, and their contents are built differently. The linker knows this, so when you ask for -lnftnl in -static mode, it doesn't even consider shared libraries.

    Ubuntu Focal's libnftnl-dev provides header files, an unversioned library symlink to the shared library provided by libnftnl11, and a few ancillary files. Neither package provides a static version of the library. This is why the linker does not find one, despite you having those packages installed.

    Your easiest way forward would be to ditch -static, and instead link against the shared libraries you already have. Here's a revised version of your makefile that specifies that, in addition to some other improvements:

    LIBS = -lmnl -lnftnl
    CFLAGS =
    prog: prog.c
        gcc $(CFLAGS) -o $@ $< $(LIBS)
    

    If you insist on building a static executable, then you'll need also to build your own copy of libnftl to provide a static version of the library. That might not be straightforward. And you might also need to do the same for libmnl.