cgo

cgo - How to convert string to C fixed char array

会有一股神秘感。 提交于 2019-12-06 04:40:30
问题 I'm trying to instantiate a C struct inside my Go code. The struct is defined like this, (in an external library that I cannot modify): typedef struct { char field1[256]; } S1 In go, I did this: 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... } But it doesn't compile because: cannot use cStr (type *C.char) as type [256]C.char in

How to make stdcall from Go

旧街凉风 提交于 2019-12-06 00:06:25
I have a pointer to a COM interface and would like to take the function pointers from its virtual table and make method calls. To do this I need to make stdcall method calls. In Go how do I make a call with convention stdcall or make a call with convention stdcall in cgo? See "godoc syscall Proc" for instructions on how to call stdcall functions on windows. Be warned that *Proc.Call does allocate / deallocate memory, so, if you care about efficiency, you should use correspondent syscall.Syscall/syscall.Syscall6/syscall.Syscall9/... function instead. Alex 来源: https://stackoverflow.com/questions

how to compile Cuda source with Go language's cgo?

不打扰是莪最后的温柔 提交于 2019-12-05 19:09:29
I wrote a simple program in cuda-c and it works on eclipse nsight. This is source code: #include <iostream> #include <stdio.h> __global__ void add( int a,int b, int *c){ *c = a + b; } int main(void){ int c; int *dev_c; cudaMalloc((void**)&dev_c, sizeof(int)); add <<<1,1>>>(2,7,dev_c); cudaMemcpy(&c, dev_c, sizeof(int),cudaMemcpyDeviceToHost); printf("\n2+7= %d\n",c); cudaFree(dev_c); return 0; } Now I'm trying to use this code with Go language with cgo!!! So I wrote this new code: package main //#include "/usr/local/cuda-7.0/include/cuda.h" //#include "/usr/local/cuda-7.0/include/cuda_runtime

integer division in Go called from C

核能气质少年 提交于 2019-12-05 17:16:15
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) int { return a * b } func Div(a, b int) int { return a / b } And the C program that calls these functions

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

不打扰是莪最后的温柔 提交于 2019-12-05 12:33:08
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] = ix; // disaster: ivec has no elements } */ // #cgo LDFLAGS: -lstdc++ import "C" //import "fmt" func

go + cgo and linking

自作多情 提交于 2019-12-05 08:04:40
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 info *C.XScreenSaverInfo var display *C.Display info = C.XScreenSaverAllocInfo() display = C

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

邮差的信 提交于 2019-12-05 00:34:35
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? 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, that does not really matter. What does matter is that for a long running process, it is likely to keep the

Conditional compilation in Go

给你一囗甜甜゛ 提交于 2019-12-04 23:13:59
问题 I'm trying to write a Go wrapper using CGo for ENet. When I tried to compile my wrapper on a Mac the library was older and had a slightly different interface. 99% of the code is the same just a few C calls need to change. What is the best practice for dealing with a problem like this in Go? Is there some way to do conditional compilation or conditional imports? 回答1: Go does not have conditional compilation or conditional imports. Handle the type differences in C code. Are the [Go] authors

How to use a relative path for LDFLAGS in golang

旧城冷巷雨未停 提交于 2019-12-04 21:46:33
问题 I am trying to build a golang program which uses a static lib (.a file) the directory struct for my project as below └─testserver ├─bin ├─pkg └─src ├─logging └─testserver ├─libtest.a └─test.go the flags for cgo in test.go as below // #cgo LDFLAGS: -L /home/test/testserver/src/testserver -ltest // #include "test.h" import "C" when I am using absolute path for LDFLAGS -L, it works fines, but when I change the path to a relative path, eg // #cgo LDFLAGS: -L ./testserver -ltest and then run the

cgo - How to convert string to C fixed char array

。_饼干妹妹 提交于 2019-12-04 09:17:20
I'm trying to instantiate a C struct inside my Go code. The struct is defined like this, (in an external library that I cannot modify): typedef struct { char field1[256]; } S1 In go, I did this: 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... } But it doesn't compile because: cannot use cStr (type *C.char) as type [256]C.char in field value I've tried forcing ([256]C.char)(cStr) but it obviously doesn't work either. Is there a way