这段代码直接报错:
package main
// #include <stdio.h>
// #include <stdlib.h>
/*
void print(char *str) {
printf("%s\n", str);
}
*/
import "C"
import "unsafe"
func main() {
s := "Hello Cgo"
cs := C.CString(s)
C.print(cs)
C.free(unsafe.Pointer(cs))
}
说一下我的需求:golang 编译的 dll 动态库输出字符串
如果是这段代码:
import "C"
func main() {
Hello_world()
}
//export Hello_world
func Hello_world() string {
return "hello dart!!"
}
编译的 dll,使用 py 调用的结果是一串数字830403984
from ctypes import CDLL, c_char_p
dll = CDLL("./hello_world.dll")
r = dll.Hello_world()
print(r)
查了一下,需要把 go 的 string 转换为对应的 C 的类型,就有最开始那个问题; 给我整蒙了。。。。