英文:
golang with cgo throws error collect2: error: ld returned 1 exit status
问题
我正在编译一个包含使用cgo
集成共享C库的Golang包。在Docker镜像golang:1.15.15
和golang: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.8
,gcc (Ubuntu 8.4.0-3ubuntu2) 8.4.0
和GNU 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
来自binutils
和gcc
的版本都没有引起问题。
看起来,链接器处理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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论