简体   繁体   English

golang with cgo 抛出错误 collect2: error: ld returns 1 exit status

[英]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.15golang: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.8gcc (Ubuntu 8.4.0-3ubuntu2) 8.4.0GNU 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.就我而言,来自binutilsgccld版本都没有导致问题。

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.

相关问题 collect2:错误:ld 返回 1 个退出状态(-lcudnn) - collect2: error: ld returned 1 exit status (-lcudnn) 多个定义collect2:错误:ld在C中返回了1个退出状态 - Multiple definition collect2: error: ld returned 1 exit status in C MulVAL 中的错误:`mylval' 的多重定义; collect2: 错误: ld 返回 1 个退出状态 - Error in MulVAL: multiple definition of `mylval'; collect2: error: ld returned 1 exit status 编译文件时出错 collect2:错误:ld返回1退出状态 - Error when compiling file | collect2: error: ld returned 1 exit status Visual Studio 2015 / Linux扩展与Cygwin一起产生“ collect2:错误:ld返回1退出状态” - Visual Studio 2015/Linux extension produces a “collect2 : error : ld returned 1 exit status” with Cygwin Debian使用IPP编译OpenCV示例:/ usr / bin / ld:找不到-llibsvml.so collect2:error:ld返回1退出状态 - Debian compiling OpenCV examples with IPP: /usr/bin/ld: cannot find -llibsvml.so collect2: error: ld returned 1 exit status java对`main'collect2的未定义引用:ld返回1退出状态 - java undefined reference to `main' collect2: ld returned 1 exit status 如何解决'collect2:ld返回1退出状态'? - How to solve 'collect2: ld returned 1 exit status '? linux程序集“collect2:ld返回1退出状态” - linux assembly “collect2: ld returned 1 exit status” 错误:-ld返回1个退出状态。 - error: -ld returned 1 exit status.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM