You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下Msys2中Golang用gotk3调用GTK3/GTK4编译报错求助

问题

在Windows的Msys2环境中,按照gotk3和GTK官方指引编写Go程序,示例代码如下:

// from: https://github.com/gotk3/gotk3
package main

import (
    "github.com/gotk3/gotk3/gtk"
    "log"
)

func main() {
    // Initialize GTK without parsing any command line arguments.
    gtk.Init(nil)

    // Create a new toplevel window, set its title, and connect it to the
    // "destroy" signal to exit the GTK main loop when it is destroyed.
    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }
    win.SetTitle("Simple Example")
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    // Create a new label widget to show in the window.
    l, err := gtk.LabelNew("Hello, gotk3!")
    if err != nil {
        log.Fatal("Unable to create label:", err)
    }

    // Add the label to the window.
    win.Add(l)

    // Set the default window size.
    win.SetDefaultSize(800, 600)

    // Recursively show all widgets contained in this window.
    win.ShowAll()

    // Begin executing the GTK main loop.  This blocks until
    // gtk.MainQuit() is run.
    gtk.Main()
}

先执行pacman -S mingw-w64-x86_64-gtk3安装GTK3,运行go build mysrc.go时出现错误:

$ go build mysrc.go
# github.com/gotk3/gotk3/glib
cgo-gcc-prolog: In function '_cgo_200740c87a77_Cfunc_g_binding_get_source':
cgo-gcc-prolog:71:2: warning: 'g_binding_get_source' is deprecated: Use 'g_binding_dup_source' instead [-Wdeprecated-declarations]
In file included from I:/msys2/mingw64/include/glib-2.0/glib-object.h:24,
                 from I:/msys2/mingw64/include/glib-2.0/gio/gioenums.h:30,
                 from I:/msys2/mingw64/include/glib-2.0/gio/giotypes.h:30,
                 from I:/msys2/mingw64/include/glib-2.0/gio/gio.h:28,
                 from I:\Go_amd64_pkgs\pkg\mod\github.com\gotk3\gotk3@v0.6.1\glib\gbinding.go:3:
I:/msys2/mingw64/include/glib-2.0/gobject/gbinding.h:115:23: note: declared here
  115 | GObject *             g_binding_get_source          (GBinding *binding);
      |                       ^~~~~~~~~~~~~~~~~~~~
cgo-gcc-prolog: In function '_cgo_200740c87a77_Cfunc_g_binding_get_target':
cgo-gcc-prolog:107:2: warning: 'g_binding_get_target' is deprecated: Use 'g_binding_dup_target' instead [-Wdeprecated-declarations]
I:/msys2/mingw64/include/glib-2.0/gobject/gbinding.h:119:23: note: declared here
  119 | GObject *             g_binding_get_target          (GBinding *binding);
      |                       ^~~~~~~~~~~~~~~~~~~~
go build github.com/gotk3/gotk3/gdk: invalid flag in pkg-config --libs: -Wl,-luuid

后续安装GTK4(pacman -S mingw-w64-x86_64-gtk4)后,编译仍报相同错误,求问题原因及解决方法。

解决方法

1. 核心问题定位

错误invalid flag in pkg-config --libs: -Wl,-luuid是关键问题:Go的cgo无法识别Msys2环境中pkg-config返回的-Wl,-luuid链接器参数,这个参数是Windows下链接uuid库的特殊写法,但cgo只接受标准的-luuid格式。

2. 具体修复步骤

  • 修改pkg-config配置文件
    找到gdk-3.0.pc文件(路径通常为/mingw64/lib/pkgconfig/gdk-3.0.pc),打开后定位到Libs行,将其中的-Wl,-luuid替换为-luuid。
    修改前示例:

    Libs: -L${libdir} -lgdk-3 -lz -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -Wl,-luuid
    

    修改后示例:

    Libs: -L${libdir} -lgdk-3 -lz -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -luuid
    
  • 清理Go模块缓存
    执行以下命令清除已缓存的gotk3编译文件,确保重新编译时使用修改后的参数:

    go clean -cache github.com/gotk3/gotk3/...
    
  • 重新编译程序
    再次运行go build mysrc.go,此时编译应能正常完成。

3. 废弃API警告说明

g_binding_get_source和g_binding_get_target的废弃警告不影响程序编译与运行,原因是当前使用的gotk3版本(v0.6.1)尚未适配GLib的新API。若要消除警告,可等待gotk3官方更新,或自行修改源码替换为g_binding_dup_source和g_binding_dup_target。


内容的提问来源于stack exchange,提问作者rnso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 13:15:33