将Golang与XLib链接

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

Linking Golang with XLib

问题

我正在尝试使用以下代码在Go中使用XLib:

package main

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

func main() {
    var dpy = C.XOpenDisplay(nil);
    if dpy == nil {
        panic("无法打开显示器")
    }

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

我通过以下方式进行编译:

go tool cgo $(FILE)

但是会出现以下错误信息:

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)

有任何解决方法吗?

英文:

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

package main

// #cgo LDFLAGS: -lX11
// #include &lt;X11/Xlib.h&gt;
import (
    &quot;C&quot;
    &quot;fmt&quot;
)

func main() {
    var dpy = C.XOpenDisplay(nil);
    if dpy == nil {
        panic(&quot;Can&#39;t open display&quot;)
    }

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

I'm compiling this via:

go tool cgo $(FILE)

But it results in the following error messages:

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

Any idea how to solve this?

答案1

得分: 8

cgo对格式要求很严格:你需要将“C”导入单独保留,并将前导注释放在其上方:

package main

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

import (
    "fmt"
)

func main() {

    var dpy = C.XOpenDisplay(nil)
    if dpy == nil {
        panic("无法打开显示器")
    }

    fmt.Println("%ix%i", C.XDisplayWidth(dpy, 0), C.XDisplayHeight(dpy, 0));
}
英文:

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 &lt;X11/Xlib.h&gt;
import &quot;C&quot;

import (
    &quot;fmt&quot;
)

func main() {

    var dpy = C.XOpenDisplay(nil)
    if dpy == nil {
        panic(&quot;Can&#39;t open display&quot;)
    }

    fmt.Println(&quot;%ix%i&quot;, C.XDisplayWidth(dpy, 0), C.XDisplayHeight(dpy, 0));
}

答案2

得分: 2

首先,除非你有特定的原因,否则不建议直接使用go tool cgo,继续像对待不使用cgo的项目一样使用go build

其次,你的cgo参数需要直接附加到"C"导入语句上,所以代码应该如下所示:

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

import (
  // your other imports
)
英文:

First of all, you do not want to use go tool cgo directly, unless you have specific reasons for doing so. Continue to use go build like you would for projects that do not use cgo.

Second, your cgo parameters need to be attached directly to the "C" import, so it has to read

// #cgo LDFLAGS: -lX11
// #include &lt;X11/Xlib.h&gt;
import &quot;C&quot;

import (
  // your other imports
)

huangapple
  • 本文由 发表于 2013年8月15日 00:53:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/18237738.html
匿名

发表评论

匿名网友

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

确定