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

huangapple go评论91阅读模式
英文:

Unable to create static binary because of undefined reference to dbus_*

问题

当我尝试静态链接使用Gopacket的Go程序时,我遇到了以下错误:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): 在函数 `dbus_write' 中:
(.text+0x103): 对 `dbus_message_demarshal' 的引用未定义
/usr/bin/ld: (.text+0x119): 对 `dbus_connection_send' 的引用未定义
/usr/bin/ld: (.text+0x122): 对 `dbus_connection_flush' 的引用未定义
/usr/bin/ld: (.text+0x12a): 对 `dbus_message_unref' 的引用未定义
/usr/bin/ld: (.text+0x178): 对 `dbus_error_free' 的引用未定义
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): 在函数 `dbus_read' 中:
(.text+0x1c3): 对 `dbus_connection_pop_message' 的引用未定义
/usr/bin/ld: (.text+0x1e1): 对 `dbus_connection_pop_message' 的引用未定义
/usr/bin/ld: (.text+0x1f6): 对 `dbus_connection_read_write' 的引用未定义
/usr/bin/ld: (.text+0x262): 对 `dbus_message_is_signal' 的引用未定义
/usr/bin/ld: (.text+0x27f): 对 `dbus_message_marshal' 的引用未定义
/usr/bin/ld: (.text+0x2e3): 对 `dbus_free' 的引用未定义
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): 在函数 `dbus_cleanup' 中:
(.text+0x350): 对 `dbus_connection_unref' 的引用未定义
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libpcap.a(pcap-dbus.o): 在函数 `dbus_activate' 中:
(.text+0x3fa): 对 `dbus_connection_open' 的引用未定义
/usr/bin/ld: (.text+0x412): 对 `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 版本。

英文:

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.

答案1

得分: 1

这些文件并不在libpcap中,而是由该版本的libpcap调用。

为了将该版本的libpcap与任何程序(无论是Go还是其他语言)进行静态链接,您需要使用pkg-config --libs --static libdpdk报告的所有库进行链接。

英文:

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

答案2

得分: 0

似乎无法创建一个使用Gopacket的静态链接二进制文件。

我更新了构建命令以使用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): 在函数 `_dbus_listen_systemd_sockets' 中:
(.text+0x200e): 对 `sd_listen_fds' 的未定义引用
/usr/bin/ld: (.text+0x204f): 对 `sd_is_socket' 的未定义引用
collect2: error: ld 返回了状态 1

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

英文:

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.

huangapple
  • 本文由 发表于 2022年11月24日 09:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/74554704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定