duanjia7912 2018-07-28 01:35
浏览 105

如何在ARM中将返回的uint8_u转换为GoString?

I call a C function from Go using cgo. The return type of the function is uint8_u *. I know it is a string and need to print it in Go.

I have the following in 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 + "
")
}

In file cLogic.h

#include <stdint.h>

int8_t *MyCFunction(char *myString, int myInt);

In File cLogic.c

#include <stdint.h>

int8_t *MyCFunction(char *myString, int myInt){
    return "this is test";
}

I get an error in the line

goResult := C.GoString(cResult)

cannot use cResult (type *_Ctype_schar) as type *_Ctype_char in argument to _Cfunc_GoString

I understand it has a casting issue, but if I cast uint8_u * to char * in C it is fine (I'm sure I won't have issues by this cast).

When I cast it on my amd64 pc with "go version go1.10.3 linux/amd64" it builds, although on an arm machine with "go version go1.10.3 linux/arm" I get the error

cannot convert cResult (type *_Ctype_schar) to type *_Ctype_char

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-07-28 08:06
    关注

    So add the wrapper

    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);
    }
    

    And include this wrapper ino your go code

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题