dongwuzun4630 2014-11-10 20:28
浏览 69
已采纳

意外的类型:…在Go中使用cgo

I'm new to Go and trying to learn how to call C from Go. I wrote this program to open a named semaphore, get the value and print it to the screen. When I run it go build semvalue.go I get the error: ./semvalue.go:16:14: unexpected type: ...

What does this mean? What am I doing wrong?

package main

import "fmt"

// #cgo LDFLAGS: -pthread
// #include <stdlib.h>
// #include <fcntl.h>
// #include <sys/stat.h>
// #include <semaphore.h>
import "C"

func main() {
    name := C.CString("/fram")
    defer C.free(name)

    fram_sem := C.sem_open(name, C.O_CREAT, C.mode_t(0644), C.uint(1))
    var val int
    ret := C.sem_getvalue(fram_sem, val)
    fmt.Println(val)
    C.sem_close(fram_sem)
}

Thank you.

  • 写回答

1条回答 默认 最新

  • dongye1912 2014-11-10 21:09
    关注

    The message is confusing, until you realize that the ... is the variadic portion of a C function. You can't use C variadic functions directly from Go, so you'll have to write a small wrapper in C to call sem_open.

    A couple more notes:

    • C.free should be called with C.free(unsafe.Pointer(name))
    • val needs to be a *C.int
    • sem_getvalue uses errno, so you should call it with ret, err := C.sem_getvalue...
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了