在使用Go语言中的gco通过gtk使用glib时遇到了一些问题。

huangapple go评论86阅读模式
英文:

Troubles using glib through gtk in Go with gco

问题

我对C的理解非常差。我可以阅读代码,但不知道如何包含/构建/制作/配置任何东西。这可能是我无法编译以下Go代码的原因。这段代码是我尝试将https://developer.gnome.org/gtk3/3.0/gtk-getting-started.html适应到Go的尝试。

package main

// #cgo pkg-config: gtk+-3.0
// #include <gtk/gtk.h>
import "C"

func main() {
    C.gtk_init(nil, nil)
    window := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
    C.g_signal_connect(window, "destroy", C.G_CALLBACK(C.gtk_main_quit), nil)
    C.gtk_widget_show(window)
    C.gtk_main()
}

有问题的一行是C.g_signal_connect(...)。错误如下:

1: error: 'G_CALLBACK' undeclared (first use in this function)
1: error: 'g_signal_connect' undeclared (first use in this function)
1: note: each undeclared identifier is reported only once for each function it appears in

如果我删除这行,那么代码可以工作,gtk窗口会打开。

我发现这个g_signal_connect来自glib-object.h,它包含在许多gtk的头文件中。我尝试自己包含它:

// #cgo pkg-config: gtk+-3.0 glib-2.0
// #include <gtk/gtk.h>
// #include <glib-object.h>

但这并没有解决任何问题。

有人知道我做错了什么吗?

英文:

My understanding of C is quite poor. I can read the code, but I have no idea how to include/build/make/configure anything. This is probably why I do not manage to get the following Go code to compile. This code my attempt at adapting https://developer.gnome.org/gtk3/3.0/gtk-getting-started.html to Go.

package main

// #cgo pkg-config: gtk+-3.0
// #include &lt;gtk/gtk.h&gt;
import &quot;C&quot;

func main() {
    C.gtk_init(nil, nil)
    window := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
    C.g_signal_connect(window, &quot;destroy&quot;, C.G_CALLBACK(C.gtk_main_quit), nil)
    C.gtk_widget_show(window)
    C.gtk_main()
}

The offending line is C.g_signal_connect(...). The errors are:

1: error: &#39;G_CALLBACK&#39; undeclared (first use in this function)
1: error: &#39;g_signal_connect&#39; undeclared (first use in this function)
1: note: each undeclared identifier is reported only once for each function it appears in

If I remove the line, then the code works and the gtk windows opens.

I figured out that this g_signal_connect comes from glib-object.h, which is included in many header files of gtk. I tried to include it myself:

// #cgo pkg-config: gtk+-3.0 glib-2.0
// #include &lt;gtk/gtk.h&gt;
// #include &lt;glib-object.h&gt;

but it did not solve anything.

Does anybody know what I am doing wrong?

答案1

得分: 1

你试图调用的函数可能实际上是宏,cgo无法处理宏,因此未定义。请查看go-gtk,它为Go提供了适当的GTK绑定。

英文:

The functions you're trying to call are probably actually macros, which are not handled by cgo and are therefore undefined. Take a look at go-gtk, which provides proper bindings to GTK for Go.

huangapple
  • 本文由 发表于 2013年6月14日 17:48:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/17105694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定