美文网首页
cgo 调用 与 c/c++混合编译

cgo 调用 与 c/c++混合编译

作者: 知成 | 来源:发表于2022-11-30 17:54 被阅读0次

go 调用 c/c++代码时

在 go 代码中调用,c/c++的静态库或动态库时,需要在在调用头文件之前的注释中,注明所依赖的或所使用的所有库信息

/*
#cgo CFLAGS: -I []
#cgo LDFAGS: -L [libPath] -l[libName]
*/

-I:指定头文件查找目录;
-L:后指明所依赖库的目录,相对路径或绝对路径都可以;
-l:后指明依赖的库名称,去掉生成库时的前缀“lib”,与后缀“.so”、“.a”;

坑坑

  • C/C++的 cons 之坑:
    cgo 无法对 const 类型进行转换;
  • import "C"之坑:
    import "C" 若与注释符号内包含的头文件链接信息之间存在空行,则可能会出现:
    “colud not determine kind of name for C.xxxx”问题;
  • cgo 调用 C/C++头文件依赖之坑:
  1. 若 C 文件中包含 C 包含的数据类型,且没有将对应的头文件包含在:extern "C"之间时,cgo 调用会引发类型错误,但是编译 C/C++为动态库不会出现问题;
//#include<stdbool.h>
#ifdef __cplusplus
extern "C"{
#endif
#include<stdbool.h>
bool func() {
printf("this is test");
return true;
}
#ifdef __cplusplus
}
#endif
  1. 若在 cgo 中没有通过 CFLAGS 指定头文件所在的目录,那么即便编译动态库或静态库时,指定了依赖方式且编译成功。在 cgo 中调用也是会出现找不到对应文件或目录之类的错误描述的;
  • go-c 指针转换之坑:
    • go 的指针转换到 c 的指针转换过程中,若 go 指针所指向的内从依旧 go 分配的内存地址,则直接会导致崩溃;具体的错误提示可能是:
      "panic: runtime error:cgo argument has Go pointer to Go poionter"
    • 根据 C 语言返回的指针地址,以及 size 信息,读取:
    • 参考:cgo类型转换
import "C"
import(
        "reflect"
        "unsafe"
)

pointer := C.GetData() // *uint8    C 接口返回的指针数据
size := C.GetSize() //size    C接口返回的指针所指内容的长度数据
var tempBuffer []byte    //临时的缓存 buffer,目的是让 tempBuffer pointer 所指的地址
var dataInfo = (*reflect.SliceHeader)(unsafe.Pointer(&tempBuffer))
dataInfo.Data = uintprt(unsafe.Pointer(pointer))
dataInfo.Len = int(size)     // 设置 len
dataInfo.Cap = int(size)    // 设置 cap
targetBuffer := make([]byte, dataInfo.Len, dataInfo.Cap)
copy(targetBuffer, tempBuffer)
C.free(unsafe.Pointer(pointer))//根据自己的使用场景,判断是否进行释放内存

写的比较粗略,后续完善;

相关文章

网友评论

      本文标题:cgo 调用 与 c/c++混合编译

      本文链接:https://www.haomeiwen.com/subject/agjsfdtx.html