使用Fyne构建Go应用时遇-lopengl32等链接错误求助
Fyne框架编译链接错误:找不到-lgdi32和-lopengl32库(架构不兼容)
问题描述
使用Fyne框架基础模板搭建应用,执行go run main.go时触发链接错误,提示无法找到-lgdi32和-lopengl32库,系统中现有库因架构不兼容被跳过。已尝试go mod tidy、go clean -modcache操作及更新显卡驱动,问题仍未解决,期望成功编译并运行指定的Fyne窗口。
报错信息
PS C:\Users\kmall\OneDrive\Documents\GitHub\acme-vdb> go run main.go # command-line-arguments C:\Program Files (x86)\Go\pkg\tool\windows_386\link.exe: running gcc failed: exit status 1 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libgdi32.a when searching for -lgdi32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../..\libgdi32.a when searching for -lgdi32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libgdi32.a when searching for -lgdi32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgdi32: No such file or directory C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libgdi32.a when searching for -lgdi32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../..\libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopengl32: No such file or directory C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../..\libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopengl32: No such file or directory C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../..\libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lopengl32: No such file or directory C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.1.0/../../../libopengl32.a when searching for -lopengl32 collect2.exe: error: ld returned 1 exit status
基础模板代码
package main import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/widget" ) func main() { a := app.New() w := a.NewWindow("Hello") hello := widget.NewLabel("Hello Fyne!") w.SetContent(container.NewVBox( hello, widget.NewButton("Hi!", func() { hello.SetText("Welcome :)") }), )) w.ShowAndRun() }
环境信息
- GCC版本:14.1.0
- Go环境变量:
set GO111MODULE=on set GOARCH=386 set GOBIN= set GOCACHE=C:\Users\kmall\AppData\Local\go-build set GOENV=C:\Users\kmall\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=386 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\Users\kmall\go\pkg\mod set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=C:\Users\kmall\go set GOPRIVATE= set GOPROXY=https://proxy.golang.org,direct set GOROOT=C:\Program Files (x86)\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLCHAIN=auto set GOTOOLDIR=C:\Program Files (x86)\Go\pkg\tool\windows_386 set GOVCS= set GOVERSION=go1.22.4 set GCCGO=gccgo set GO386=sse2 set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=C:\Users\kmall\OneDrive\Documents\GitHub\acme-vdb\go.mod set GOWORK= set CGO_CFLAGS=-O2 -g set CGO_CPPFLAGS= set CGO_CXXFLAGS=-O2 -g set CGO_FFLAGS=-O2 -g set CGO_LDFLAGS=-O2 -g set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m32 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\kmall\AppData\Local\Temp\go-build1144242527=/tmp/go-build -gno-record-gcc-switches
解决方案
问题根源
当前使用的是32位Go(GOARCH=386),但搭配的是64位MinGW(mingw64)编译器。链接器尝试用64位版本的libgdi32.a和libopengl32.a链接32位程序,因架构不兼容被跳过,最终找不到对应32位的库文件。
方案一:切换为64位Go + 64位MinGW
- 卸载当前32位Go,下载并安装64位版本的Go
- 确保mingw64的
gcc可执行文件在系统PATH中 - 执行以下命令设置Go架构为64位:
若需永久生效,可在系统环境变量中添加set GOARCH=amd64GOARCH=amd64 - 重新执行
go run main.go
方案二:使用32位MinGW配合32位Go
- 通过MSYS2安装32位MinGW:
pacman -S mingw-w64-i686-gcc - 设置环境变量指定32位编译器:
set CC=i686-w64-mingw32-gcc - 执行
go run main.go
内容的提问来源于stack exchange,提问作者Kyle Malling
相关产品推荐
相关产品推荐

