记录初次使用go gtk3 制作gui时遇到的天坑问题(废弃函数被包含在了第三方库中)

记录初次使用go gtk3 制作gui时遇到的天坑问题(废弃函数被包含在了第三方库中)

gotk3是go 语言的第三方库,用于绑定go语言和gtk3,今天尝试使用了一下。

这篇博客记录我初次使用时遇到的编译问题以及解决方法

新版本的glib2(2.68.1.1及以上版本)的接口与gotk3库的函数不兼容的问题

F:\Golang_study\src\github.com\gotk3\gotk3\_examples\boolprops>go build boolprops.go
# github.com/gotk3/gotk3/glib
cgo-gcc-prolog: In function '_cgo_e48307fa3ad4_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 E:/msys64/mingw64/include/glib-2.0/glib-object.h:22,
                 from E:/msys64/mingw64/include/glib-2.0/gio/gioenums.h:28,
                 from E:/msys64/mingw64/include/glib-2.0/gio/giotypes.h:28,
                 from E:/msys64/mingw64/include/glib-2.0/gio/gio.h:26,
                 from ..\..\glib\gbinding.go:3:
E:/msys64/mingw64/include/glib-2.0/gobject/gbinding.h:112:23: note: declared here
  112 | GObject *             g_binding_get_source          (GBinding *binding);
      |                       ^~~~~~~~~~~~~~~~~~~~
cgo-gcc-prolog: In function '_cgo_e48307fa3ad4_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]
In file included from E:/msys64/mingw64/include/glib-2.0/glib-object.h:22,
                 from E:/msys64/mingw64/include/glib-2.0/gio/gioenums.h:28,
                 from E:/msys64/mingw64/include/glib-2.0/gio/giotypes.h:28,
                 from E:/msys64/mingw64/include/glib-2.0/gio/gio.h:26,
                 from ..\..\glib\gbinding.go:3:
E:/msys64/mingw64/include/glib-2.0/gobject/gbinding.h:116:23: note: declared here
  116 | GObject *             g_binding_get_target          (GBinding *binding);

这个问题使用时在windows平台上使用msys2安装GTK3与go语言绑定时,你一定会遇到(目前这个版本)

这是由于glib-2.0\gobject\gbin\gbinding.h里将几个函数做了替换,如以下代码

GLIB_DEPRECATED_IN_2_68_FOR(g_binding_dup_source)
GObject *             g_binding_get_source          (GBinding *binding);
GLIB_AVAILABLE_IN_2_68
GObject *             g_binding_dup_source          (GBinding *binding);
GLIB_DEPRECATED_IN_2_68_FOR(g_binding_dup_target)
GObject *             g_binding_get_target          (GBinding *binding);
GLIB_AVAILABLE_IN_2_68
GObject *             g_binding_dup_target          (GBinding *binding);
GLIB_AVAILABLE_IN_ALL
const gchar *         g_binding_get_source_property (GBinding *binding);
GLIB_AVAILABLE_IN_ALL
const gchar *         g_binding_get_target_property (GBinding *binding);

里面的g_binding_get_source ,g_binding_get_target,g_binding_get_source_property分别被g_binding_dup_source ,g_binding_dup_target,g_binding_get_target_property替代,但是第三方库中却没有为此修改,如github.com\gotk3\gotk3@v0.6.1\glib\gbinding.go中还是沿用了旧代码,并没把废弃代码替换,所以我们只需要将废弃代码替换就可以正常运行了

package glib

// #include <gio/gio.h>
// #include <glib.h>
// #include <glib-object.h>
// #include "glib.go.h"
import "C"
import "unsafe"

type BindingFlags int

const (
	BINDING_DEFAULT        BindingFlags = C.G_BINDING_DEFAULT
	BINDING_BIDIRECTIONAL  BindingFlags = C.G_BINDING_BIDIRECTIONAL
	BINDING_SYNC_CREATE                 = C.G_BINDING_SYNC_CREATE
	BINDING_INVERT_BOOLEAN              = C.G_BINDING_INVERT_BOOLEAN
)

type Binding struct {
	*Object
}

func (v *Binding) native() *C.GBinding {
	if v == nil || v.GObject == nil {
		return nil
	}
	return C.toGBinding(unsafe.Pointer(v.GObject))
}

func marshalBinding(p uintptr) (interface{}, error) {
	c := C.g_value_get_object((*C.GValue)(unsafe.Pointer(p)))
	return &Binding{wrapObject(unsafe.Pointer(c))}, nil
}

// Creates a binding between source property on source and target property on
// target . Whenever the source property is changed the target_property is
// updated using the same value.
func BindProperty(source *Object, sourceProperty string,
	target *Object, targetProperty string,
	flags BindingFlags) *Binding {
	srcStr := (*C.gchar)(C.CString(sourceProperty))
	defer C.free(unsafe.Pointer(srcStr))
	tgtStr := (*C.gchar)(C.CString(targetProperty))
	defer C.free(unsafe.Pointer(tgtStr))
	obj := C.g_object_bind_property(
		C.gpointer(source.GObject), srcStr,
		C.gpointer(target.GObject), tgtStr,
		C.GBindingFlags(flags),
	)
	if obj == nil {
		return nil
	}
	return &Binding{wrapObject(unsafe.Pointer(obj))}
}

// Explicitly releases the binding between the source and the target property
// expressed by Binding
func (v *Binding) Unbind() {
	C.g_binding_unbind(v.native())
}

// Retrieves the GObject instance used as the source of the binding
func (v *Binding) GetSource() *Object {
	// obj := C.g_binding_get_source(v.native())
	obj := C.g_binding_dup_source(v.native())
	if obj == nil {
		return nil
	}
	return wrapObject(unsafe.Pointer(obj))
}

// Retrieves the name of the property of “source” used as the source of
// the binding.
func (v *Binding) GetSourceProperty() string {
	// s := C.g_binding_get_source_property(v.native())
	s := C.g_binding_get_target_property(v.native())
	return C.GoString((*C.char)(s))
}

// Retrieves the GObject instance used as the target of the binding.
func (v *Binding) GetTarget() *Object {
	// obj := C.g_binding_get_target(v.native())
	obj := C.g_binding_dup_target(v.native())
	if obj == nil {
		return nil
	}
	return wrapObject(unsafe.Pointer(obj))
}

// Retrieves the name of the property of “target” used as the target of
// the binding.
func (v *Binding) GetTargetProperty() string {
	s := C.g_binding_get_target_property(v.native())
	return C.GoString((*C.char)(s))
}

// Retrieves the flags passed when constructing the GBinding.
func (v *Binding) GetFlags() BindingFlags {
	flags := C.g_binding_get_flags(v.native())
	return BindingFlags(flags)
}

这是我修改好的。

这样我们的这个问题就解决了、

修改D:\msys64\mingw64\lib\pkgconfig中的文件

可以参考gotk2中的.pc文件修改方法就可以了

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值