如何在Windows上使用cgo构建适用于386架构的库?

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

How build lib for 386 arch with cgo on windows?

问题

我有一个在Linux、MacOS和Windows上构建和运行良好的golang库。问题出现在我尝试在amd64 Windows虚拟机上为386构建它时。我已经安装了最新的golang SDK和mingw,这使得amd64构建正常工作,但386构建不起作用:

如何修复它?据我所知,应该可以在同一台机器上为两个架构构建。

英文:

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.

答案1

得分: 0

Cgo构建失败,我通过以下方式解决:首先,确保成功构建C代码。

通常,cgo交叉编译需要C交叉编译和Go交叉编译。也许你可以添加设置// #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

答案2

得分: 0

我已经找到了项目描述中的差异,并切换到https://www.mingw-w64.org/。它包含了386和x64两种架构的库。现在我的交叉编译工作正常。

英文:

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.

huangapple
  • 本文由 发表于 2022年7月9日 19:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/72920810.html
匿名

发表评论

匿名网友

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

确定