如何在cgo导出的函数中获取正确的参数名称? [英] How do I get proper parameter names in cgo exported functions?

查看:89
本文介绍了如何在cgo导出的函数中获取正确的参数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Go中编写一个库,我想将其导出到C共享库.它工作正常,但是我发现导出的标头使用 p0 p1 p2 有点烦人...代替参数名称,而不是Go中的原始参数名称.有没有办法改变这种行为,还是我只是坚持下去?

我正在使用 go版本go1.12.7 darwin/amd64

示例:

 程序包主要/*#import< stdlib.h>*/导入"C"进口 ("fmt")func main(){}//导出MyFuncfunc MyFunc(input * C.char){fmt.Println(C.GoString(input));} 

 开始构建-o libout.so -buildmode = c-shared 

输出:

  extern void MyFunc(char * p0); 

为什么没有将 p0 命名为 input ?

根据此cgo文档,我应该获取变量名./p>

状态

可以通过以下方式导出Go函数以供C代码使用:

 //导出MyFunctionfunc MyFunction(arg1,arg2 int,arg3字符串)int64 {...}//导出MyFunction2func MyFunction2(arg1,arg2 int,arg3字符串)(int64,* C.char){...} 

它们在C代码中的显示方式为:

  extern int64 MyFunction(int arg1,int arg2,GoString arg3);extern struct MyFunction2_return MyFunction2(int arg1,int arg2,GoString arg3); 

但是,当我编译它给出的确切代码时,会得到以下结果:

  extern GoInt64 MyFunction(GoInt p0,GoInt p1,GoString p2);extern struct MyFunction2_return MyFunction2(GoInt p0,GoInt p1,GoString p2); 

为什么参数没有名称?

解决方案

Go允许使用任意符文名称,例如,您可以调用变量π来代替 input .C不允许这样的名称.大概是cgo的作者不想限制您的名字,所以他们只是重命名了所有内容.

奇怪的是,文档暗示生成的代码将使用您的姓名.如果您愿意的话,这样做的话就很好了.

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) {
    fmt.Println(C.GoString(input));
}

go build -o libout.so -buildmode=c-shared

Output:

extern void MyFunc(char* p0);

Why isn't p0 named input?

According to this cgo documentation, i should be getting variable names.

It States

Go functions can be exported for use by C code in the following way:

//export MyFunction
func MyFunction(arg1, arg2 int, arg3 string) int64 {...}

//export MyFunction2
func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}

They will be available in the C code as:

extern int64 MyFunction(int arg1, int arg2, GoString arg3);
extern struct MyFunction2_return MyFunction2(int arg1, int arg2, GoString arg3);

However, when I compile that exact code it gave, i get this result:

extern GoInt64 MyFunction(GoInt p0, GoInt p1, GoString p2);
extern struct MyFunction2_return MyFunction2(GoInt p0, GoInt p1, GoString p2);

Why don't the parameters have their names?

解决方案

Go allows arbitrary rune names, e.g., instead of input you might call the variable π. C does not allow such names. Presumably the cgo authors didn't want to restrict your names, so they just renamed everything.

Curiously, the documentation implies that the generated code will use your names. It would be nice if it did do that, provided that your names are viable.

这篇关于如何在cgo导出的函数中获取正确的参数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆