duankange2433 2013-07-26 11:17
浏览 109
已采纳

golang:我可以在软件包之间共享C.int

in the main package i have:

var foo C.int
foo = 3
t := fastergo.Ctuner_new()
fastergo.Ctuner_register_parameter(t, &foo, 0, 100, 1)

in the fastergo package i have:

func Ctuner_register_parameter(tuner unsafe.Pointer, parameter *C.int, from C.int, to C.int, step C.int) C.int {
    ...
}

if i try to run it, i get:

demo.go:14[/tmp/go-build742221968/command-line-arguments/_obj/demo.cgo1.go:21]: cannot use &foo (type *_Ctype_int) as type *fastergo._Ctype_int in function argument

i am not really sure what go is trying to tell me here, but somehow i think it wants to tell me, that all C.int are not equal? why is this the case? how can i solve this / work around?

  • 写回答

1条回答 默认 最新

  • dongzhui4927 2013-07-26 23:50
    关注

    Since _Ctype_int doesn't begin with a Unicode upper case letter, the type is local to the package. Use Go types, except in the C wrapper package where you convert them to C types. The wrapper package should hide all the implementation details.

    You don't provide sufficient information for us to create sample code which compiles and runs. Here's a rough outline of what I expected to see:

    package main
    
    import "tuner"
    
    func main() {
        var foo int
        foo = 3
        t := tuner.New()
        t.RegisterParameter(&foo, 0, 100, 1)
    }
    

    .

    package tuner
    
    import (
        "unsafe"
    )
    
    /*
    #include "ctuner.h"
    */
    import "C"
    
    type Tuner struct {
        ctuner uintptr
    }
    
    func New() *Tuner {
        var t Tuner
        t.ctuner = uintptr(unsafe.Pointer(C.ctuner_new()))
        return &t
    }
    
    func (t *Tuner) RegisterParameter(parameter *int, from, to, step int) error {
        var rv C.int
        rv = C.ctuner_register_parameter(
            (*C.ctuner)(unsafe.Pointer(t.ctuner)),
            (*C.int)(unsafe.Pointer(parameter)),
            C.int(from),
            C.int(to),
            C.int(step),
        )
        if rv != 0 {
            // handle error
        }
        return nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?