首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由于未定义对dbus_*的引用,无法创建静态二进制文件

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

Stack Overflow用户
提问于 2022-11-24 01:31:42
回答 2查看 54关注 0票数 0

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

代码语言:javascript
复制
/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,要么显示为未定义的符号。例如:

代码语言:javascript
复制
$ 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

代码语言:javascript
复制
$ 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]

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

代码语言:javascript
复制
GOOS=linux CGO_ENABLED=1 go build \
    -ldflags "-linkmode external -extldflags \"-static\"" \
    -o bin/myprog \
    -buildvcs=false

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

我用的是Go 1.18。

EN

回答 2

Stack Overflow用户

发布于 2022-11-24 06:31:05

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

它们不属于libpcap。它们被那个版本的libpcap所调用。

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

票数 0
EN

Stack Overflow用户

发布于 2022-11-24 15:37:00

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

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

代码语言:javascript
复制
GOOS=linux CGO_ENABLED=1 CGO_LDFLAGS="-ldbus-1" go build \
    -ldflags "-linkmode external -extldflags \"-static\"" \
    -o bin/app \
    -buildvcs=false

但是,我得到了以下错误:

代码语言:javascript
复制
/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的无静态库

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74554704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档