如何在Windows上使用CGO构建386 Arch的Lib?

发布于 2025-02-14 02:16:15 字数 981 浏览 0 评论 0 原文

我有一个Golang库,可以在Linux,MacOS和Windows上构建和效果很好。当我试图在AMD64 Windows VM上以386的形式构建它时,问题出在问题。我已经安装了最新的Golang SDK和MingW,这使AMD64构建工作正常,但不工作386:

PS > gcc -v
gcc.exe (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 11.2.0

PS > go version
go version go1.18.3 windows/amd64
    
PS > $Env:GOOS = "windows"; $Env:GOARCH = "386"; $Env:CGO_ENABLED ="1"; go build -v -buildmode=c-shared -ldflags="-s -w" -gcflags="-l" -o xyz_amd64.dll xyz_win_dll.go
...
runtime/cgo
c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/pro
gramdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/libmingwthrd.a when searching for -lmingwt
hrd
...
<a lot of skipping incompatible messages here>
collect2.exe: error: ld returned 1 exit status

如何修复它? Afaik应该可以在同一盒子上为两个拱门建造。

I have a golang library that builds and works well on Linux, MacOs and Windows. The problem comes when I'm trying to build it for 386 on the amd64 Windows VM. I've installed latest golang SDK and mingw, which makes amd64 build work fine, but not the 386:

PS > gcc -v
gcc.exe (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 11.2.0

PS > go version
go version go1.18.3 windows/amd64
    
PS > $Env:GOOS = "windows"; $Env:GOARCH = "386"; $Env:CGO_ENABLED ="1"; go build -v -buildmode=c-shared -ldflags="-s -w" -gcflags="-l" -o xyz_amd64.dll xyz_win_dll.go
...
runtime/cgo
c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/pro
gramdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/lib/libmingwthrd.a when searching for -lmingwt
hrd
...
<a lot of skipping incompatible messages here>
collect2.exe: error: ld returned 1 exit status

How to fix it? AFAIK it should be possible to build for both arch on the same box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2025-02-21 02:16:15

CGO构建失败,我以这种方式解决它:首先,确保成功构建C代码。

通常,CGO交叉汇编需要C交叉汇编并进行交叉汇编。也许您可以添加SET // #CGO CFLAGS:C_CROSSBUILD_PARAMETERS

如您所见,这并不容易。这就是为什么交叉汇编不在窗口

Cgo build fail, I solve it by this way: firstly, ensure build C code successfully.

generally, cgo cross compilation require C cross compilation and Go cross compilation . maybe you can add set // #cgo CFLAGS:C_CrossBuild_Parameters.

As you can see, it's not easy. It's why Cross compilation goes out the window

梦里兽 2025-02-21 02:16:15

我在项目描述中找到了差异,然后切换到 https://www.mingw.mingw.mingw.mingw.mingw.mingw.mingw64.org/ 。它包含Arch 386和X64的Libs。现在,整个汇编对我来说很好。

I've found the diff in the project description and switched to https://www.mingw-w64.org/. It contains libs for both arch 386 and x64. Now across compilation works fine to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文