去+ CGO和链接

i want to use the following c as Go's cgo:

#include <X11/extensions/scrnsaver.h>

main() {
  XScreenSaverInfo *info = XScreenSaverAllocInfo();
  Display *display = XOpenDisplay(0);

  XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
  printf("%u ms
", info->idle);
}

build with:

gcc -o idle printXIdleTime.c -lX11 -lXss

i re-wrote that code for Go's cgo:

package tools

// #cgo pkg-config: x11
// #include <X11/extensions/scrnsaver.h>
import "C"

func GetIdleTime() (idleTime uint32) {
    var info *C.XScreenSaverInfo
    var display *C.Display 

    info = C.XScreenSaverAllocInfo()
    display = C.XOpenDisplay(0)

    defaultRootWindow := C.XDefaultRootWindow(display)

    C.XScreenSaverQueryInfo(display, defaultRootWindow, info)
    idleTime = info.idle

    return
}

tried to compile with:

go build -gccgoflags="-lXss -lX11"

however i'm getting linker errors:

/tmp/go-build076004816/opensource.stdk/lib/tools/_obj/x11.cgo2.o: In function _cgo_c0e279f6f16e_Cfunc_XScreenSaverAllocInfo': ./x11.go:52: undefined reference toXScreenSaverAllocInfo' /tmp/go-build076004816/opensource.stdk/lib/tools/_obj/x11.cgo2.o: In function _cgo_c0e279f6f16e_Cfunc_XScreenSaverQueryInfo': ./x11.go:65: undefined reference toXScreenSaverQueryInfo' collect2: error: ld returned 1 exit status

what am i doing wrong?

2个回答

This is how I got it to build. Note the #cgo LDFLAGS line which is probably what you are missing. I had to make a few other changes to make it build. It seems to be returning the right answer on my Linux machine!

package tools

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

func GetIdleTime() (idleTime uint32) {
    var info *C.XScreenSaverInfo
    var display *C.Display

    info = C.XScreenSaverAllocInfo()
    display = C.XOpenDisplay(nil)

    defaultRootWindow := C.XDefaultRootWindow(display)

    C.XScreenSaverQueryInfo(display, C.Drawable(defaultRootWindow), info)
    idleTime = uint32(info.idle)

    return
}



请注意,可以使用本机 X Go绑定和ScreenSaver扩展名:</ p>

 包main 

import(
“ fmt”
“ log”
\ n“ github.com/BurntSushi/xgb"
” github.com/BurntSushi/xgb/xproto"
“ github.com/BurntSushi/xgb/screensaver"
)

func main(){
X ,err:= xgb.NewConn()
如果err!= nil {
log.Fatal(err)
}

如果err = screensaver.Init(X); err!= nil {
log.Fatal(err)
}

root:= xproto.Setup(X).DefaultScreen(X).Root
info,err:= screensaver.QueryInfo(X,xproto .Drawable(root))。Reply()
if err!= nil {
log.Fatal(err)
}

fmt.Printf(“ Idle:%d
”,info.MsSinceUserInput)

}
</ code> </ pre>

输出:</ p>

  [andrew @ Liger go]转到运行xscreensaver_idle.go 
Idle: 1040
</ code> </ pre>
</ div>

展开原文

原文

Note that this can be done with the native X Go Binding and the ScreenSaver extension:

package main

import (
    "fmt"
    "log"

    "github.com/BurntSushi/xgb"
    "github.com/BurntSushi/xgb/xproto"
    "github.com/BurntSushi/xgb/screensaver"
)

func main() {
    X, err := xgb.NewConn()
    if err != nil {
        log.Fatal(err)
    }

    if err = screensaver.Init(X); err != nil {
        log.Fatal(err)
    }

    root := xproto.Setup(X).DefaultScreen(X).Root
    info, err := screensaver.QueryInfo(X, xproto.Drawable(root)).Reply()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Idle: %d
", info.MsSinceUserInput)
}

Outputs:

[andrew@Liger go] go run xscreensaver_idle.go 
Idle: 1040

dongmei8760
dongmei8760 XGB应该发出警告消息,但仍然可以连接。 您可以提交包含测试用例和环境描述的错误报告吗? github.com/BurntSushi/xgb/issues
7 年多之前 回复
duanbangzhou7809
duanbangzhou7809 gos XGB的问题是,如果我在家中没有.XAuthority,NewConn()将失败,并显示消息“无法连接”
7 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐