我正在嘗試使用以下代碼在 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 in1: error: 'XDisplayWidth' undeclared (first use in this function)1: error: 'XDisplayHeight' undeclared (first use in this function)知道如何解決這個問題嗎?
2 回答

LEATH
TA貢獻1936條經驗 獲得超7個贊
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));
}

一只萌萌小番薯
TA貢獻1795條經驗 獲得超7個贊
首先,你不想go tool cgo直接使用,除非你有特定的理由這樣做。go build像不使用 cgo 的項目一樣繼續使用。
其次,你的 cgo 參數需要直接附加到“C”導入,所以它必須讀取
// #cgo LDFLAGS: -lX11
// #include <X11/Xlib.h>
import "C"
import (
// your other imports
)
- 2 回答
- 0 關注
- 331 瀏覽
添加回答
舉報
0/150
提交
取消