doupang1917 2013-08-14 16:53
浏览 64
已采纳

将Golang与XLib链接

I am trying to use XLib within Go using this code:

package main

// #cgo LDFLAGS: -lX11
// #include <X11/Xlib.h>
import (
    "C"
    "fmt"
)

func main() {
    var dpy = C.XOpenDisplay(nil);
    if dpy == nil {
        panic("Can't open display")
    }

    fmt.Println("%ix%i", C.XDisplayWidth(), C.XDisplayHeight());
}

I'm compiling this via:

go tool cgo $(FILE)

But it results in the following error messages:

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

Any idea how to solve this?

  • 写回答

2条回答 默认 最新

  • dongzhang5006 2013-08-14 17:12
    关注

    cgo is picky about the formatting: you need to keep the "C" import separate, and place the preamble comments immediately above:

    package main
    
    // #cgo LDFLAGS: -lX11
    // #include <X11/Xlib.h>
    import "C"
    
    import (
        "fmt"
    )
    
    func main() {
    
        var dpy = C.XOpenDisplay(nil)
        if dpy == nil {
            panic("Can't open display")
        }
    
        fmt.Println("%ix%i", C.XDisplayWidth(dpy, 0), C.XDisplayHeight(dpy, 0));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码