doubu1950 2013-06-14 09:48
浏览 82
已采纳

使用gco在Go中通过gtk使用glib的问题

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条回答 默认 最新

  • dongyan7950 2013-06-14 10:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵