I get these errors when I try to statically link my Go program that uses Gopacket:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): in function `dbus_write':
(.text 0x103): undefined reference to `dbus_message_demarshal'
/usr/bin/ld: (.text 0x119): undefined reference to `dbus_connection_send'
/usr/bin/ld: (.text 0x122): undefined reference to `dbus_connection_flush'
/usr/bin/ld: (.text 0x12a): undefined reference to `dbus_message_unref'
/usr/bin/ld: (.text 0x178): undefined reference to `dbus_error_free'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): in function `dbus_read':
(.text 0x1c3): undefined reference to `dbus_connection_pop_message'
/usr/bin/ld: (.text 0x1e1): undefined reference to `dbus_connection_pop_message'
/usr/bin/ld: (.text 0x1f6): undefined reference to `dbus_connection_read_write'
/usr/bin/ld: (.text 0x262): undefined reference to `dbus_message_is_signal'
/usr/bin/ld: (.text 0x27f): undefined reference to `dbus_message_marshal'
/usr/bin/ld: (.text 0x2e3): undefined reference to `dbus_free'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): in function `dbus_cleanup':
(.text 0x350): undefined reference to `dbus_connection_unref'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): in function `dbus_activate':
(.text 0x3fa): undefined reference to `dbus_connection_open'
/usr/bin/ld: (.text 0x412): undefined reference to `dbus_bus_register'
...
Indeed these symbols indeed either do not exist /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a
or show up as undefined. For example:
$ readelf -s /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a | grep dbus_message_marshal
42: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND dbus_message_marshal
None of these functions are called from my program, but are happening because of the dependency to Gopacket.
I have libpcap
installed:
$ apt list --installed|grep pcap
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libpcap-dev/jammy,now 1.10.1-4build1 amd64 [installed]
libpcap0.8-dev/jammy,now 1.10.1-4build1 amd64 [installed]
libpcap0.8/jammy,now 1.10.1-4build1 amd64 [installed,automatic]
Is there anything else I need? Here's how I compile:
GOOS=linux CGO_ENABLED=1 go build \
-ldflags "-linkmode external -extldflags \"-static\"" \
-o bin/myprog \
-buildvcs=false
If I do not include -ldflags
, the program compiles, but it is not statically linked.
I am using Go 1.18.
CodePudding user response:
do not exist /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a
They are not in libpcap. They are called by that version of libpcap.
You will have to link with all of the libraries reported by pkg-config --libs --static libdpdk
in order to statically link that version of libpcap with any program whether it's in Go or not.
CodePudding user response:
It's seems like it is impossible to make a statically linked binary that uses Gopacket.
I updated the build command to use libdbus-1. This got rid of the undefined dbus_* errors:
GOOS=linux CGO_ENABLED=1 CGO_LDFLAGS="-ldbus-1" go build \
-ldflags "-linkmode external -extldflags \"-static\"" \
-o bin/app \
-buildvcs=false
However, I got this error:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libdbus-1.a(libdbus_1_la-dbus-sysdeps-unix.o): in function `_dbus_listen_systemd_sockets':
(.text 0x200e): undefined reference to `sd_listen_fds'
/usr/bin/ld: (.text 0x204f): undefined reference to `sd_is_socket'
collect2: error: ld returned 1 exit status
These functions come from libsystemd. I installed libsystemd-dev
, but it seems like there is no static library for libsystemd.