英文:
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 <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()
}
The offending line is C.g_signal_connect(...)
. The errors are:
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
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 <gtk/gtk.h>
// #include <glib-object.h>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论