golang与cgo一起使用时出现错误:collect2: error: ld returned 1 exit status。

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

golang with cgo throws error collect2: error: ld returned 1 exit status

问题

我正在编译一个包含使用cgo集成共享C库的Golang包。在Docker镜像golang:1.15.15golang:1.16.6中,一切都能成功构建,但是从golang:1.16.7开始(也包括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

我查看了Golang的发布说明,没有找到与cgo相关的任何重要更改。

我检查了gcc和ld的版本,它们都是不同的。我甚至设置了一个带有go1.13.8gcc (Ubuntu 8.4.0-3ubuntu2) 8.4.0GNU ld (GNU Binutils for Ubuntu) 2.34的Ubuntu发行版,结果仍然遇到了这个问题,所以我猜测问题不是由go引起的。

你有什么线索或建议,我如何找到这个问题的根本原因?检查gcc和ld是否正确,还是需要调查其他工具?

英文:

I am compiling a golang package, which includes the integration of a shared c library using cgo.
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:

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

I checked versions of gcc and ld, those are all different. 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.

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?

答案1

得分: 0

感谢Zyl,我能够缩小问题范围。

我检查了几个发行版(bullseye、buster、stretch),在bullseye上构建失败。在我的情况下,ld来自binutilsgcc的版本都没有引起问题。

看起来,链接器处理DT_NEEDED标签的默认设置已经改变。我解决了我的问题,采用了https://stackoverflow.com/a/62117174/2290153上的解决方案,并在环境中添加了export CGO_LDFLAGS=-Wl,--no-as-needed。根据https://manpages.debian.org/bullseye/binutils-common/gold.1.en.html,这是ld.gold链接器的默认设置,但不适用于ld

对我帮助很大的是go build命令的-x标志,可以查看为cgo执行的gcc命令。

英文:

Thanks to Zyl, I was able to narrow down the problem.

I checked several distributions (bullseye, buster, stretch) and with bullseye, the build failed. In my case, neither the version of ld coming from binutils nor gcc caused the problem.

It seems, that the default settings for handling DT_NEEDED tags for the linker have changed. 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. 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.

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.

huangapple
  • 本文由 发表于 2021年10月8日 20:20:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/69495750.html
匿名

发表评论

匿名网友

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

确定