如何从Go语言中使用Cgo工具调用这个C函数

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

how to call this C function from Go language (with Cgo tool)

问题

这是一个C函数声明:

CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);

我该如何从Go中调用这个函数?

type Easy struct {
    curl unsafe.Pointer
    code C.CURLcode
}

func (e *Easy) SetOption(option C.CURLoption, ...) {
    e.code = C.curl_easy_setopt(e.curl, option, ????)
}
英文:

Here is this C function declaration

CURL_EXTERN CURLcode curl_easy_setopt(CURL *curl, CURLoption option, ...);

how do I call this function from Go?

type Easy struct {
    curl unsafe.Pointer
    code C.CURLcode
}

func (e *Easy)SetOption(option C.CURLoption, ...) {
    e.code = C.curl_easy_setopt(e.curl, option, ????))
}

答案1

得分: 4

你不能直接调用它。CGO在C端与可变参数函数不兼容。
理想情况下,你可以创建一个C包装器,接受你想要传递的选项列表。
然后,C函数应该将该列表扩展为curl_easy_set_opt()所需的可变参数。但我不确定是否可能或如何去做。

你的Go函数的签名也是不正确的:

type Option C.CURLoption

func (e *Easy) SetOption(options ...Option) {
    // 'options'现在可以作为一个切片访问:[]Option
    // 将这个切片转换为C.CURLoption指针的列表,并将其传递给
    // 你的C包装器。
    
    if len(options) == 0 {
        return // 没有继续的用处。
    }
    
    // 这是一种将选项切片转换为C可以使用的列表的方法。
    size := int(unsafe.Sizeof(options[0]))
    list := C.malloc(C.size_t(size * len(options)))
    defer C.free(unsafe.Pointer(list)) // 使用后释放!
    
    for i := range options {
        ptr := unsafe.Pointer( uintptr(list) + uintptr(size * i) )
        *(*C.CURLoption)(ptr) = C.CURLoption(options[i])
    }
    
    C.my_setopt_wrapper(e.curl, list, C.int(len(options)))
}

请注意,选项参数的类型已更改为其go版本。当有人使用你的包时,他们无法访问C.xxx类型。所以你不应该在你的公共API中使用它们。它们只用于你包内部使用。

英文:

You can't call it directly. CGO does not play well with vararg functions on the C side.
Ideally, you could create a C wrapper which accepts a list of options you want to pass.
The C function should then expand that list into the variable arguments required by curl_easy_set_opt(). But I am not sure if that is possible or how to go about doing it.

The signature for your Go function is also incorrect:

type Option C.CURLoption

func (e *Easy) SetOption(options ...Option) {
    // 'options' is now accessible as a slice: []Option
    // Turn this slice into a list of C.CURLoption pointers and pass it to
    // your C wrapper.
    
    if len(options) == 0 {
        return // No use continuing.
    }
    
    // Here is one way to convert the option slice to a list
    // that C can work with.
    size := int(unsafe.Sizeof(options[0]))
    list := C.malloc(C.size_t(size * len(options)))
    defer C.free(unsafe.Pointer(list)) // Free this after use!
    
    for i := range options {
        ptr := unsafe.Pointer( uintptr(list) + uintptr(size * i) )
        *(*C.CURLoption)(ptr) = C.CURLoption(options[i])
    }
    
    C.my_setopt_wrapper(e.curl, list, C.int(len(options)))
}

Note that the type of the option parameter has been changed to a go version of it. When someone uses your package, they have no access to the C.xxx types. So you should not use those in your public api. They are only meant for internal use in your package.

huangapple
  • 本文由 发表于 2011年8月4日 22:04:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/6942837.html
匿名

发表评论

匿名网友

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

确定