由于未定义对dbus_*的引用,无法创建静态二进制文件



当我尝试静态链接使用Gopacket:的Go程序时,会出现这些错误

/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'
...

事实上,这些符号实际上要么不存在/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a,要么显示为未定义。例如:

$ 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

这些函数都不是从我的程序中调用的,而是由于对Gopacket的依赖而发生的。

我安装了libpcap

$ 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]

我还需要什么吗?以下是我的编译方法:

GOOS=linux CGO_ENABLED=1 go build 
-ldflags "-linkmode external -extldflags "-static"" 
-o bin/myprog 
-buildvcs=false

如果我不包括-ldflags,程序会编译,但它不是静态链接的。

我正在使用Go 1.18。

不存在/usr/lib/gcc/x86_64-linux-gnu/11/../..//x86_64-linux-gnu/libpcap.a

它们不是libpcap中的。它们由libpcap的版本调用。

您必须链接到pkg-config --libs --static libdpdk报告的所有库,才能将该版本的libpcap与任何程序静态链接,无论该程序是否在Go中。

似乎不可能制作一个使用Gopacket的静态链接二进制文件。

我更新了build命令以使用libdbus-1。这消除了未定义的dbus_*错误:

GOOS=linux CGO_ENABLED=1 CGO_LDFLAGS="-ldbus-1" go build 
-ldflags "-linkmode external -extldflags "-static"" 
-o bin/app 
-buildvcs=false

然而,我得到了这个错误:

/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

这些函数来自libsystemd。我安装了libsystemd-dev,但似乎没有用于libsystemd的静态库。

最新更新