gotk3是go 语言的第三方库,用于绑定go语言和gtk3,今天尝试使用了一下。
这篇博客记录我初次使用时遇到的编译问题以及解决方法
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)
}
这是我修改好的。
这样我们的这个问题就解决了、
可以参考gotk2中的.pc文件修改方法就可以了