centos8下搭建gotk3(go语言gtk库) x86_64-w64-mingw32交叉编译环境

1. 配置软件源

  • 本地源:
baseurl=file:///media/cdrom/BaseOS
baseurl=file:///media/cdrom/AppStream
  • PowerTools 源:
baseurl=http://mirrors.aliyun.com/centos/$releasever/PowerTools/$basearch/os
  •  epel 源:
baseurl=http://mirrors.aliyun.com/epel/8/Everything/$basearch
  • puias 源(包含mingw64-gtk3等dll库):
baseurl=http://46.17.45.37/puias/8.1/x86_64/os/AppStream/Packages

2. 安装编译工具

yum install -y mingw64-gcc
yum install -y mingw64-gcc-c++
yum install -y mingw64-glib2
yum install -y mingw64-gtk3 (版本号:3.22.30)

修改链接参数,否则会报错,执行

sed -i  's/-Wl,-luuid/-luuid/g' /usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig/gdk-3.0.pc

3. x86_64-w64-mingw32编译安装gotk3

PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig \
CGO_ENABLED=1 \
CC=x86_64-w64-mingw32-gcc \
GOOS=windows \
GOARCH=amd64 \
go install github.com/gotk3/gotk3/gtk

4. 编译代码

// main.c
package main

import (
	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
	"os"
)

func main() {
	const appId = "com.nayoso.example"

	app, _ := gtk.ApplicationNew(appId, glib.APPLICATION_FLAGS_NONE)
	app.Connect("activate", func() {
		onActivate(app)
	})
	app.Run(os.Args)
}

func onActivate(application *gtk.Application) {
	appWindow, _ := gtk.ApplicationWindowNew(application)
	appWindow.SetTitle("Grid example")

	grid, _ := gtk.GridNew() //创建容器
	appWindow.Add(grid)      //将容器添加到window中

	//创建一些按钮来展示grid的效果
	button1, _ := gtk.ButtonNewWithLabel("Button 1")
	button2, _ := gtk.ButtonNewWithLabel("Button 2")
	button3, _ := gtk.ButtonNewWithLabel("Button 3")
	//将buttons添加到grid中
	grid.Attach(button1, 0, 0, 1, 1) //参数:左,上,宽,高
	grid.Attach(button2, 1, 0, 1, 1)
	grid.Attach(button3, 0, 1, 2, 1)

	appWindow.ShowAll()
}

编译:

# If you do need a CMD window with your app's GUI (e.g. for easier user-side debugging),
# remove the -ldflags -H=windowsgui option.
PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig \
CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 \
go build -ldflags -H=windowsgui

5. windows安装gtk运行时环境:gtk3-runtime_3.22(最接近3.22.30的版本)

https://master.dl.sourceforge.net/project/gtkd-packages/gtk3-runtime/gtk3-runtime_3.22.4_64-bit.exe

将上一步生成的main.exe拷贝到Windows ,双击即可看到下面的界面


 

PS: centos8搭建nsis交叉编译环境,参见另一篇文章:https://blog.csdn.net/hbuxiaofei/article/details/105995059

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页