[英]golang with cgo throws error collect2: error: ld returned 1 exit status
I am compiling a golang package, which includes the integration of a shared c library using cgo
.我正在编译一个 golang 包,其中包括使用
cgo
集成一个共享的 c 库。 Everything builds successfully inside docker images golang:1.15.15
, golang:1.16.6
, but since golang:1.16.7
(also golang:1.17
) it fails with error:一切都在
golang:1.15.15
images golang:1.15.15
, golang:1.16.6
golang:1.16.7
内成功构建,但由于golang:1.16.7
golang:1.17
(也是golang:1.17
)它失败并出现错误:
/usr/bin/ld: src/foobar/lib/libXYZ.so: undefined reference to `feenableexcept'
/usr/bin/ld: src/foobar/lib/libXYZ.so: undefined reference to `floor'
...
/usr/bin/ld: src/foobar/lib/libXYZ.so: undefined reference to `memoFree'
/usr/bin/ld: src/foobar/lib/libXYZ.so: undefined reference to `memoMalloc'
collect2: error: ld returned 1 exit status
I checked the golang release notes, and could not find any relevant changes for cgo
.我查看了 golang 发行说明,找不到
cgo
任何相关更改。
I checked versions of gcc and ld, those are all different.我检查了 gcc 和 ld 的版本,这些都是不同的。 I even setup a ubuntu distro with
go1.13.8
, gcc (Ubuntu 8.4.0-3ubuntu2) 8.4.0
and GNU ld (GNU Binutils for Ubuntu) 2.34
, where I run into this issue, so I guess, that go
goes not cause it.我甚至安装Ubuntu的发行版与
go1.13.8
, gcc (Ubuntu 8.4.0-3ubuntu2) 8.4.0
和GNU ld (GNU Binutils for Ubuntu) 2.34
,在那里我遇到了这个问题,所以我想,那go
那张没有原因它。
Do you have any clue or suggestion, how I can find the root cause of this issue?您有任何线索或建议,我如何才能找到此问题的根本原因? Is it right to check gcc and ld, or which other tools need investigation?
检查gcc和ld是否正确,或者其他哪些工具需要调查?
Thanks to Zyl, I was able to narrow down the problem.感谢 Zyl,我能够缩小问题的范围。
I checked several distributions (bullseye, buster, stretch) and with bullseye, the build failed.我检查了几个发行版(bullseye、buster、stretch),使用bullseye,构建失败了。 In my case, neither the version of
ld
coming from binutils
nor gcc
caused the problem.就我而言,来自
binutils
和gcc
的ld
版本都没有导致问题。
It seems, that the default settings for handling DT_NEEDED
tags for the linker have changed.看起来,用于处理链接器的
DT_NEEDED
标签的默认设置已更改。 I resolved my problem taking the solution from https://stackoverflow.com/a/62117174/2290153 and adding export CGO_LDFLAGS=-Wl,--no-as-needed
to the environment.我解决了我的问题,从https://stackoverflow.com/a/62117174/2290153获取解决方案并将
export CGO_LDFLAGS=-Wl,--no-as-needed
到环境中。 According to https://manpages.debian.org/bullseye/binutils-common/gold.1.en.html this is the default for the ld.gold
linker, but not for ld
.根据https://manpages.debian.org/bullseye/binutils-common/gold.1.en.html,这是
ld.gold
链接器的默认值,但不是ld
的默认值。
What helped me a lot, was the -x
flag of the go build
command to have a look at the gcc command being executed for cgo
.对我帮助很大的是
go build
命令的-x
标志,用于查看为cgo
执行的 gcc 命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.