【问题标题】:Call Go function with string parameter from C?从C调用带有字符串参数的Go函数?
【发布时间】:2022-02-08 18:02:44
【问题描述】:

我可以从 C per below 调用不带参数的 Go 函数。这通过go build 编译并打印

Hello from Golang main function!
CFunction says: Hello World from CFunction!
Hello from GoFunction!

ma​​in.go

package main

//extern int CFunction();
import "C"
import "fmt"

func main() {
  fmt.Println("Hello from Golang main function!")
  //Calling a CFunction in order to have C call the GoFunction
  C.CFunction();
}

//export GoFunction
func GoFunction() {
  fmt.Println("Hello from GoFunction!")
}

file1.c

#include <stdio.h>
#include "_cgo_export.h"

int CFunction() {
  char message[] = "Hello World from CFunction!";
  printf("CFunction says: %s\n", message);
  GoFunction();
  return 0;
}

现在,我想将一个字符串/字符数组从 C 传递给 GoFunction。

根据cgo documentation中的“C references to Go”这是可能的,所以我在GoFunction中添加了一个字符串参数并将char数组message传递给GoFunction:

ma​​in.go

package main

//extern int CFunction();
import "C"
import "fmt"

func main() {
  fmt.Println("Hello from Golang main function!")
  //Calling a CFunction in order to have C call the GoFunction
  C.CFunction();
}

//export GoFunction
func GoFunction(str string) {
  fmt.Println("Hello from GoFunction!")
}

file1.c

#include <stdio.h>
#include "_cgo_export.h"

int CFunction() {
  char message[] = "Hello World from CFunction!";
  printf("CFunction says: %s\n", message);
  GoFunction(message);
  return 0;
}

go build 我收到此错误:

./file1.c:7:14: error: passing 'char [28]' to parameter of incompatible type 'GoString'
./main.go:50:33: note: passing argument to parameter 'p0' here

根据上面"C? Go? Cgo!" blog post的“字符串和事物”部分:

Go 和 C 字符串之间的转换是通过 C.CString、C.GoString 和 C.GoStringN 函数完成的。

但是这些是在 Go 中使用的,如果我想将字符串数据传递到 Go 中没有帮助。

【问题讨论】:

  • 如果你阅读下面的文档,将会生成一个_cgo_export.h ,类型为GoString,你可以使用它。它看起来像:typedef struct { const char *p; GoInt n; } GoString

标签: c go cgo


【解决方案1】:

C 中的字符串是 *C.char,而不是 Go string。 让导出的函数接受正确的 C 类型,并在 Go 中根据需要进行转换:

//export GoFunction
func GoFunction(str *C.char) {
    fmt.Println("Hello from GoFunction!")
    fmt.Println(C.GoString(str))
}

【讨论】:

  • 我认为 OP 是在问,如何调用只接受 Go 字符串的 Go 函数。但我想创建一个包装函数是一种方法。
  • @Ainar-G:好点子,我应该等待评论回复;)
  • 这行得通!当我在故障排除时尝试此操作时,我错误地使用了str *C.Char,导致错误could not determine kind of name for C.Char
  • @JoePrvacy 如果您确实需要 str 成为 string,请查看我的答案。
【解决方案2】:

如果你想将一个 C 字符串传递给一个只接受 Go 字符串的函数,你可以在 C 端使用GoString 类型:

char message[] = "Hello World from CFunction!";
printf("CFunction says: %s\n", message);
GoString go_str = {p: message, n: sizeof(message)}; // N.B. sizeof(message) will
                                                    // only work for arrays, not
                                                    // pointers.
GoFunction(go_str);
return 0;

【讨论】:

  • 非常感谢!我投了赞成票,但它不会显示在柜台上,因为我的代表很低。
猜你喜欢
  • 2011-09-08
  • 2021-04-23
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
  • 2014-07-29
  • 2012-07-01
  • 1970-01-01
  • 2011-09-01
相关资源
最近更新 更多