Tag: cgo

cgo结果已经指针

我正在编写一些导出类似函数的代码: package main import “C” //export returnString func returnString() string { // gostring := “hello world” return gostring } func main() {} 我使用go build -buildmode = c-shared构建.so和头文件,但是当我在我的C代码中调用returnString()时,我得到“恐慌:运行时错误:cgo结果有Go指针” 在1.9中有没有办法解决这个问题?

CGO:如何在Golang中使用指针从C中访问数组中的数据

我正在使用FFmpeg编写一个用于Windows平台的应用程序,它是golang包装器goav,但是我无法理解如何使用C指针来访问他们指向的数据数组。 我正在尝试将数据存储在AVFrame类中,并使用Go将其写入文件,最后使用OpenGl中的纹理来制作具有很酷转换的video播放器。 我认为理解如何投射和访问C数据将使编码变得更加容易。 我已经删除了C代码的所有相关部分,包装器和我的代码,如下所示: C代码 – libavutil / frame.h #include typedef struct AVFrame { #define AV_NUM_DATA_POINTERS 8 uint8_t *data[AV_NUM_DATA_POINTERS]; } Golang goav包装器 – 我真的不知道这里有什么不安全的.Pointers和cast但它让我可以访问底层的C代码 package avutil /* #cgo pkg-config: libavutil #include #include */ import “C” import ( “unsafe” ) type Frame C.struct_AVFrame func AvFrameAlloc() *Frame { return (*Frame)(unsafe.Pointer(C.av_frame_alloc())) } func Data(f *Frame) *uint8 { return […]

如何从Golang访问C指针数组

我正在使用FFmpeg编写一个用于Windows平台的应用程序,它是golang包装器goav,但我无法理解如何使用C指针来获取对数组的访问权限。 我正在尝试将存储在AVFormatContext类中的流用于go,并最终将帧添加到OpenGl中的纹理以使video播放器具有很酷的过渡。 我认为理解如何投射和访问C数据将使编码变得更加容易。 我已经删除了C代码的所有相关部分,包装器和我的代码,如下所示: C代码 – libavformat / avformat.h typedef struct AVFormatContext { unsigned int nb_streams; AVStream **streams; } Golang goav包装纸 package avutil //#cgo pkg-config: libavformat //#include import “C” import ( “unsafe” ) type Context C.struct_AVFormatContext; func (ctxt *Context) StreamsGet(i uintptr) *Stream { streams := (**Stream)(unsafe.Pointer(ctxt.streams)); // I think this is where it’s going wrong, […]

如何使用指向C数据的指针在Golang中写入文件?

我正在使用FFmpeg编写一个用于Windows平台的应用程序,它是golang包装器goav,但我无法理解如何使用C指针来获取对数组的访问权限。 我正在尝试将来自C的uint8指针指向的帧数据写入golang中的.ppm文件。 一旦我完成了这个,为了certificateFFmpeg正在做我期望的概念,我想在OpenGl中将帧设置为纹理,以使video播放器具有很酷的过渡; 任何指向做得好而有效的指针将非常有帮助! 我猜我需要写一些着色器代码来绘制ppm作为纹理…… PPM文件结构看起来非常简单,只有标题,然后是从左上角到右下角的帧中每个像素的每个红色,绿色和蓝色值的数据字节 我开始理解如何在C和Go类型之间转换指针,但是我如何访问数据并在Go中写入与C相同的结果? 在CI中,只需设置数据的指针偏移量并说明要写入多少: for (y = 0; y data[0]+y*pFrame->linesize[0], 1, width*3, pFile); } 我已经删除了C代码的所有相关部分,包装器和我的代码,如下所示: C代码 – libavutil / frame.h #include typedef struct AVFrame { #define AV_NUM_DATA_POINTERS 8 uint8_t *data[AV_NUM_DATA_POINTERS]; int linesize[AV_NUM_DATA_POINTERS]; } Golang goav包装纸 package avutil /* #cgo pkg-config: libavutil #include #include */ import “C” import ( “unsafe” ) type […]

如何在go中将指针传递给切片到C函数

背景:使用cgo从Golang调用C函数。 我想使用具有此签名的C函数: int f(int *count, char ***strs) 。 它将修改count和strs的数据,这就是它使用指针的原因。 count的值是strs的长度; strs是一个字符串数组; 返回值只是一个(布尔)指示符,指示是否存在错误。 在golang中,我可以通过使用Cf((*C.int)(&count))成功传递和修改count ; 使用[]*C.char传递[]string 。 示例代码如下: /* #include int f(int *c, char **str) { int i; printf(“%d\n”, *c); for (i = 0; i < *c; i++) { printf("%s\n", str[i]); } *c = (*c) + 1; return 1; } */ import "C" func go_f(strs []string) […]

cgo – 如何将字符串转换为C固定字符数组

我正在尝试在Go代码中实例化一个C结构。 结构是这样定义的(在我无法修改的外部库中): typedef struct { char field1[256]; } S1 在去,我做了这个: func myfunc(mystr string){ // We need to convert mystr from string to char array cStr := C.CString(mystr) defer C.free(unsafe.Pointer(cStr) // Should work now s1 := &C.S1{field1: cStr} // Do something with s1… } 但它没有编译,因为: 不能在字段值中使用cStr(类型* C.char)作为类型[256] C.char 我试过强迫([256] C.char)(cStr),但它显然也不起作用。 有没有办法实现我想要做的事情?

为什么cgo的性能如此之慢? 我的测试代码有问题吗?

我正在做一个测试:比较cgo和纯Go函数的执行时间各自运行1亿次。 与Golang函数相比,cgo函数需要更长的时间,我对此结果感到困惑。 我的测试代码是: package main import ( “fmt” “time” ) /* #include #include #include void show() { } */ // #cgo LDFLAGS: -lstdc++ import “C” //import “fmt” func show() { } func main() { now := time.Now() for i := 0; i < 100000000; i = i + 1 { C.show() } end_time := time.Now() […]