cgo

How do I write to a file in Golang using a pointer to the C data?

ⅰ亾dé卋堺 提交于 2019-12-10 19:16:58
问题 I'm writing an app for the windows platform using FFmpeg and it's golang wrapper goav, but I'm having trouble understanding how to use the C pointers to gain access to an array. I'm trying to write the frame data, pointed to by a uint8 pointer from C, to a .ppm file in golang. Once I have this done, for proof of concept that FFmpeg is doing what I expect it to, I want to set the frames to a texture in OpenGl to make a video player with cool transitions; any pointers to do that nice and

How can I fill out void* C pointer in Go?

ぐ巨炮叔叔 提交于 2019-12-10 15:19:02
问题 I am trying to interface with some C code from Go. Using cgo, this has been relatively straight-forward until I hit this (fairly common) case: needing to pass a pointer to a structure that itself contains a pointer to some data. I cannot seem to figure out how to do this from Go without resorting to putting the creation of the structure into the C code itself, which I'd prefer not to do. Here is a snippet that illustrates the problem: package main // typedef struct { // int size; // void

compile gopacket on windows 64bit

两盒软妹~` 提交于 2019-12-09 05:59:51
问题 I am trying to use gopacket on my windows 10. I'm using it to sniff and inject packets directly to/from the NIC. I can easily compile and run my code with GOARCH=386 but can't in GOARCH=amd64. Worth noticing: I am NOT trying to cross-compile. I'm using go1.6.windows-386 to compile the 32bit version and when I try to compile with GOARCH=amd64 I use go1.6.windows-amd64. I used TDM-GCC as linux like compile tools. The error isn't indicative. it just says c:/WpdPack/Lib/x64/wpcap.lib: error

cgo: Go pointers in Go memory

安稳与你 提交于 2019-12-07 18:35:27
问题 Does the code: unsafe.Pointer(&du) where du is some interface satisfy the rule 1 from the following list? https://github.com/golang/go/issues/12416 Go code may pass a Go pointer to C provided that the Go memory to which it points does not contain any Go pointers. That rule must be preserved during C execution, in that the program must not store any Go pointers into that memory. In other words, is the C-pointer to Go interface considered as a "pointer to Go memory containing a Go pointers"?

integer division in Go called from C

烈酒焚心 提交于 2019-12-07 08:45:47
问题 I am able to perform integer division in go by this program : package main import "fmt" func main() { a := 10 b := 5 fmt.Println(a/b) } Then I made a program in go that has functions for +, -, * and /. and I made a program in C that calls each of these functions and performs arithmetic operations. Except division, the code works fine. The go file with the functions is : (calc.go) package main func Add(a, b int) int { return a + b } func Sub(a, b int) int { return a - b } func Mul(a, b int)

How to use std::vector or other container in cgo of golang?

徘徊边缘 提交于 2019-12-07 07:33:44
问题 I want to malloc large number of objects in to memory.(about 100 million objects) because the gc of golang is not effective enough,so i need to use c/c++ to malloc memory and use std::vector to hold objects. this is my code,i want use std container in cgo: package main import ( "fmt" ) /* #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> using namespace std; void dosome(){ vector<int> ivec; // empty vector for (vector<int>::size_type ix = 0; ix != 10; ++ix) ivec[ix]

How to convert from [][]byte to **char in go

风格不统一 提交于 2019-12-07 04:44:14
问题 I would like to convert from a go [][]byte to a C **char. In other words, I have a byte matrix in go that I would like to convert to a char double pointer in C. Please assume that I HAVE to have a [][]byte as input and a **char as output. I know it is possible to convert from []byte to *char by doing something like: ((*C.char)(unsafe.Pointer(&data[0]))) But it does not seem possible to extend this case into the second dimension. I have tried something pretty elaborate, where I pack a [][]byte

go + cgo and linking

一个人想着一个人 提交于 2019-12-07 02:02:35
问题 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\n", 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

Is there a way to release unmanaged resources when a Go struct is collected?

社会主义新天地 提交于 2019-12-06 18:53:40
问题 I have a pointer to a C type wrapped by a Go struct, like so: type Wrapper struct { unmanaged *C.my_c_type } The C type, in turn, has the following functions: my_c_type* make_c_type(); void free_c_type(my_c_type *ct); Is there a way that I can ensure that free_c_type is called whenever a Wrapper instance is finalized? 回答1: You can use runtime.SetFinalizer. This allows you to run a cleanup function when the object falls out of scope. It is not guaranteed to run. However, when freeing memory,

cgo: Go pointers in Go memory

◇◆丶佛笑我妖孽 提交于 2019-12-06 11:37:38
Does the code: unsafe.Pointer(&du) where du is some interface satisfy the rule 1 from the following list? https://github.com/golang/go/issues/12416 Go code may pass a Go pointer to C provided that the Go memory to which it points does not contain any Go pointers. That rule must be preserved during C execution, in that the program must not store any Go pointers into that memory. In other words, is the C-pointer to Go interface considered as a "pointer to Go memory containing a Go pointers"? UPDATE: My issue is the following code: type Receiver interface { Signal() } func WowFunction(data []byte