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

2022-11-30  本文已影响0人  知成

go 调用 c/c++代码时

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

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

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

坑坑

  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 中调用也是会出现找不到对应文件或目录之类的错误描述的;
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))//根据自己的使用场景,判断是否进行释放内存

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

上一篇 下一篇

猜你喜欢

热点阅读