cgo

How do I get proper parameter names in cgo exported functions?

血红的双手。 提交于 2020-07-22 06:26:29
问题 I'm writing a library in Go that I want to export to a c-shared-library. It works just fine, however i find it a little annoying that the exported header uses p0 , p1 , p2 , ... for parameter names instead of the original parameter names from Go. Is there a way to change this behavior or am I simply stuck with that? I am using go version go1.12.7 darwin/amd64 Example: package main /* #import <stdlib.h> */ import "C" import ( "fmt" ) func main() {} //export MyFunc func MyFunc(input *C.char) {

Go slice backed by a C array [duplicate]

风格不统一 提交于 2020-07-08 03:20:49
问题 This question already has an answer here : What does (*[1 << 30]C.YourType) do exactly in CGo? (1 answer) Closed 2 years ago . In the CGO section of the Golang Wiki, there is an article that explains how to create a Go slice backed by a C array. In the article there is a code snipped that details the conversion and most important statement in that snippet is the following: slice := (*[1 << 30]C.YourType)(unsafe.Pointer(theCArray))[:length:length] Everything in the statement makes sense to me

Garbage collection and cgo

孤人 提交于 2020-01-22 09:44:07
问题 Is it possible to make the garbage collector in Go handle and release memory allocated through C code? I apologize, I haven't used C and cgo before so my examples may need some clarification. Lets say you've got some C library that you'd like to use and this library allocates some memory that needs to be freed manually. What I'd like to do is something like this: package stuff /* #include <stuff.h> */ import "C" type Stuff C.Stuff func NewStuff() *Stuff { stuff := Stuff(C.NewStuff()) //

casting a cgo array into a slice

左心房为你撑大大i 提交于 2020-01-11 13:11:27
问题 At the moment I do this for casting a CGO array of doubles into a slice of float64: doubleSlc := [6]C.double{} // Fill doubleSlc floatSlc := []float64{float64(doubleSlc[0]), float64(doubleSlc[1]), float64(doubleSlc[2]), float64(doubleSlc[3]), float64(doubleSlc[4]), float64(doubleSlc[5])} Is there a less cumbersome way of doing the same thing? I suppose this can also be seen as a general way of casting between slices/arrays of different types in Go. 回答1: You have the normal and safe way of

casting a cgo array into a slice

人走茶凉 提交于 2020-01-11 13:11:18
问题 At the moment I do this for casting a CGO array of doubles into a slice of float64: doubleSlc := [6]C.double{} // Fill doubleSlc floatSlc := []float64{float64(doubleSlc[0]), float64(doubleSlc[1]), float64(doubleSlc[2]), float64(doubleSlc[3]), float64(doubleSlc[4]), float64(doubleSlc[5])} Is there a less cumbersome way of doing the same thing? I suppose this can also be seen as a general way of casting between slices/arrays of different types in Go. 回答1: You have the normal and safe way of

golang cgo can't export variables by build mode c-shared

大兔子大兔子 提交于 2020-01-06 08:19:09
问题 I am trying to develop in the cgo a plug-in of sudo. https://www.sudo.ws/man/1.8.15/sudo_plugin.man.html export the struct to the global scope of policy_plugin. A policy plugin must declare and populate a policy_plugin struct in the global scope. Do you have an explanation of what that means? export_test.go package main /* #include "sudo_plugin.h" #include <stddef.h> */ import "C" func main() { } // don't worked //export policy var policy = &C.struct_policy_plugin{ C.SUDO_POLICY_PLUGIN, C

Trouble using exported function type with C types as parameters

假如想象 提交于 2020-01-04 01:53:07
问题 EDIT: changed the title to better reflect the problem thanks to @Not_a_Golfer I have been experimenting with Go and cannot figure out this problem. The following works fine when it is contained within the main package: // Define a callback-function type, and define an invoker for it type Callback func(i C.int, d C.double) func Invoke(cb Callback, i C.int, d C.double) { cb(i, d) } //... then define my callback and pass it into the invoker func foo(i C.int, d C.double) { fmt.Printf("i %d d %f\n

CGO undefined reference in included files

孤者浪人 提交于 2020-01-02 08:29:33
问题 Wraping up OpenJtalk in Go, files are successfully included and types are referenced without an issue, but functions trigger an undefined reference error. jtalk.go: package main // #cgo CFLAGS: -I/home/vagrant/open_jtalk/njd [...etc] /* #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <string.h> #include <math.h> // Main headers #include "mecab.h" #include "njd.h" #include "jpcommon.h" #include "HTS_engine.h" // Sub headers #include "text2mecab.h" #include "mecab2njd.h"

Calling setns from Go returns EINVAL for mnt namespace

会有一股神秘感。 提交于 2019-12-31 13:55:11
问题 The C code works fine and correctly enters the namespace, but the Go code always seems to return EINVAL from the setns call to enter the mnt namespace. I've tried a number of permutations (including embedded C code with cgo and external .so ) on Go 1.2 , 1.3 and the current tip. Stepping through the code in gdb shows that both sequences are calling setns in libc the exact same way (or so it appears to me). I have boiled what seems to be the issue down to the code below. What am I doing wrong?

Passing array of string as parameter from go to C function

感情迁移 提交于 2019-12-31 03:43:14
问题 I have one C function: int cgroup_change_cgroup_path(const char * path, pid_t pid, const char *const controllers[]) I want to call it in go language by using cgo. How to pass the third parameter as it accepts a C array of string. 回答1: You can build the arrays using c helper functions and then use them. Here is a solution to the same problem: // C helper functions: static char**makeCharArray(int size) { return calloc(sizeof(char*), size); } static void setArrayString(char **a, char *s, int n)