douhu2898 2019-08-15 15:08
浏览 110

如何在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?

  • 写回答

1条回答 默认 最新

  • douxiongye5779 2019-08-16 01:14
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况