我正在尝试使用以下代码在 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("Can't open display")
}
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)
知道如何解决这个问题吗?
最佳答案
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("Can't open display")
}
fmt.Println("%ix%i", C.XDisplayWidth(dpy, 0), C.XDisplayHeight(dpy, 0));
}
关于go - 将 Golang 与 XLib 连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18237738/