From ae8edc7624018a80ada352ccff8c5a6e01d461a7 Mon Sep 17 00:00:00 2001 From: Ondrej Mosnacek Date: Wed, 25 Sep 2019 10:56:16 +0200 Subject: [PATCH] libbpf: fix linker flags for shared library The -lelf flag needs to be specified *after* the object files, otherwise the output library produced by some compilers doesn't contain a link to libelf.so: (Example from Debian testing run on Travis.) $ ldd libelf.so linux-vdso.so.1 (0x00007ffcbfda9000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f75f8d24000) /lib64/ld-linux-x86-64.so.2 (0x00007f75f8f0f000) Linking against such library then produces 'undefined reference to ...' errors unless the target links against libelf as well. After this commit the built library references the libelf library correctly: $ ldd libbpf.so linux-vdso.so.1 (0x00007ffc821f1000) libelf.so.1 => /usr/lib/x86_64-linux-gnu/libelf.so.1 (0x00007f70ea3ec000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f70ea22c000) libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f70ea20f000) /lib64/ld-linux-x86-64.so.2 (0x00007f70ea433000) Signed-off-by: Ondrej Mosnacek --- src/Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Makefile b/src/Makefile index c3cbbe5..3f4d2b1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -77,9 +77,9 @@ $(OBJDIR)/libbpf.so.$(LIBBPF_MAJOR_VERSION): $(OBJDIR)/libbpf.so.$(LIBBPF_VERSIO ln -sf $(^F) $@ $(OBJDIR)/libbpf.so.$(LIBBPF_VERSION): $(OBJS) - $(CC) -shared $(ALL_LDFLAGS) -Wl,--version-script=$(VERSION_SCRIPT) \ - -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \ - $^ -o $@ + $(CC) -shared -Wl,--version-script=$(VERSION_SCRIPT) \ + -Wl,-soname,libbpf.so.$(LIBBPF_MAJOR_VERSION) \ + $^ $(ALL_LDFLAGS) -o $@ $(OBJDIR)/libbpf.pc: sed -e "s|@PREFIX@|$(PREFIX)|" \