我使用cgo调用Go中的C函数。函数的返回类型是uint8_u *。我知道这是一个字符串,需要打印在Go中。
我在myFile.go中有以下内容
package main
// #cgo CFLAGS: -g
// #include <stdlib.h>
// #include "cLogic.h"
import "C"
import (
"fmt"
"unsafe"
)
func main() {
myString := "DUMMY"
cMyString := C.CString(myString)
defer C.free(unsafe.Pointer(cMyString))
cMyInt := C.int(10)
cResult := C.MyCFunction(cMyString, cMyInt) // Result is type *_Ctype_schar (int8_t *)
goResult := C.GoString(cResult)
fmt.Println("GoResult: " + goResult + "\n")
}
文件cLogic.h
#include <stdint.h>
int8_t *MyCFunction(char *myString, int myInt);
在文件cLogic.c中
#include <stdint.h>
int8_t *MyCFunction(char *myString, int myInt){
return "this is test";
}
我在行中有个错误
goResult := C.GoString(cResult)
不能将cResult (*_Ctype_schar类型)用作_Cfunc_GoString参数中的*_Ctype_char类型
我知道它有一个铸造问题,但是如果我将uint8_u *转换为char *在C中是很好的(我相信我不会在这个转换中出现问题)。
当我用"go version go1.10.3 linux/ an 64“在我的amd64 pc上转换它时,它会生成,尽管在带有"go版本go1.10.3 linux/ arm”的arm机器上,我得到了错误
无法将cResult (*_Ctype_schar类型)转换为*_Ctype_char类型
发布于 2018-07-28 08:06:06
所以添加包装器
wrapper.h
#include <stdint.h>
char *mywrapper(char *myString, int myInt);
wrapper.c
#include <stdint.h>
#include "cLogic.h"
char *mywrapper(char *myString, int myInt);
{
return (char *)MyCFunction(myString, myInt);
}
并包括这个包装器和你的围棋代码
https://stackoverflow.com/questions/51567313
复制相似问题