新手搞这个坑比较多,环境会有各种问题。我测试的时候是2021年3月15日(此时go最新版本是1.16.2 ; msys2-20210228 gtk2 gtk3 等都是用的最新的),我开始用的go是1.15+ ,但是语法有不兼容的地方。后来把go降级到go version go1.14.15 windows/amd64 ,build demo才成功。1.15会提示各种语法弃用,build无法完成。我的测试demo用的是github.com/gotk3/gotk3。
gtk在windows安装需要借助msys2,gtk安装的官方文档https://www.gtk.org/docs/installations/windows/ ;msys2 和gtk go 等只是开发软件时需要安装,运行时只要有打包后的可执行文件即可。
注意,msys2安装的位置使用默认的c盘,不要更改(第三次安装时,我直接在默认路径安装还是出现问题,现在不确定哪个环节出的问题,也不确定怎么搞好了),否则安装后pkg-config --cflags gtk+-2.0 测试会报错,找不到gtk+-2.0等,如果安装位置不是默认位置需要修改msys2 的内部的配置文件,让pkg-config找到绝对位置(安装在其他磁盘目录修改配置文件我没深入研究,在windows环境变量设置pkg-config的路径不好使)。
默认安装,下一步即可。安装完立即运行MSYS 64bit
#在MSYS2的控制台中执行下面的命令
pacman -S mingw-w64-x86_64-gtk3
pacman -S mingw-w64-x86_64-gtk2
pacman -S mingw-w64-x86_64-glade
pacman -S mingw-w64-x86_64-toolchain base-devel
配置windows环境变量PATH,增加C:\msys64\usr\bin 和C:\msys64\mingw64\bin 两个path路径。
测试安装是否成功
pkg-config --cflags gtk+-2.0
make -v
例子来自https://github.com/gotk3/gotk3
package main
import (
"log"
"os"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
// Simple Gtk3 Application written in go.
// This application creates a window on the application callback activate.
// More GtkApplication info can be found here -> https://wiki.gnome.org/HowDoI/GtkApplication
func main() {
// Create Gtk Application, change appID to your application domain name reversed.
const appID = "org.gtk.example"
application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE)
// Check to make sure no errors when creating Gtk Application
if err != nil {
log.Fatal("Could not create application.", err)
}
// Application signals available
// startup -> sets up the application when it first starts
// activate -> shows the default first window of the application (like a new document). This corresponds to the application being launched by the desktop environment.
// open -> opens files and shows them in a new window. This corresponds to someone trying to open a document (or documents) using the application from the file browser, or similar.
// shutdown -> performs shutdown tasks
// Setup Gtk Application callback signals
application.Connect("activate", func() { onActivate(application) })
// Run Gtk application
os.Exit(application.Run(os.Args))
}
// Callback signal from Gtk Application
func onActivate(application *gtk.Application) {
// Create ApplicationWindow
appWindow, err := gtk.ApplicationWindowNew(application)
if err != nil {
log.Fatal("Could not create application window.", err)
}
// Set ApplicationWindow Properties
appWindow.SetTitle("Basic Application.")
appWindow.SetDefaultSize(400, 400)
appWindow.Show()
}
运行报错,处理方法参考https://www.cnblogs.com/it-tsz/p/10731539.html
PS C:\Users\2019051954\Desktop\go\gossh> go build main.go
go build github.com/gotk3/gotk3/gdk: invalid flag in pkg-config --libs: -Wl,-luuid
#改:C:\msys64\mingw64\lib\pkgconfig\gdk-2.0.pc,如下:
Libs: -L${libdir} -lgdk-${target}-2.0 -lgdi32 -limm32 -lshell32 -lole32 -luuid
Cflags: -I${includedir}/gtk-2.0 -I${libdir}/gtk-2.0/include
#将原本在Libs中的-Wl删除,增加一行即可:
LDFLAGS: -Wl
#其他要修改的文件如下:
C:\msys64\mingw64\lib\pkgconfig\gdk-3.0.pc
C:\msys64\mingw64\lib\pkgconfig\gdk-broadway-3.0.pc
C:\msys64\mingw64\lib\pkgconfig\gdk-win32-2.0.pc
C:\msys64\mingw64\lib\pkgconfig\gdk-win32-3.0.pc
最终demo运行如下图所示: