【发布时间】:2020-06-04 05:15:03
【问题描述】:
我正在使用 cgo 从 go 中调用 c。我想通过函数传递一个结构。 在我的 c 文件中,它看起来像这样:
#include <stdio.h>
void test(TestStruct val) {
printf(val);
}
在我的 go 文件中,我正在这样做:
package main
// #cgo CXXFLAGS: -I/usr/lib/
// #cgo LDFLAGS: -L/usr/lib/ -lstdc++
//#include <file.c>
import "C"
type TestStruct struct {
val string
type string
}
func main() {
var value = TestStruct{ "foo", "bar" }
C.test(value);
}
但这不起作用。它抛出这个错误:
无法确定 C.test 的名称类型
【问题讨论】:
-
除了 C 代码不知道正确的类型之外,Go 字符串不是 C 字符串,并且您不能将包含 Go 分配的指针的值传递给 C。