質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top