pointers - 将 GO 指针传递给 C 回调函数
问题描述
我需要将函数指针作为参数(即回调函数)传递给共享对象内的 C 函数。期望是从共享对象内部的函数中获取回调。我需要在 GO 中实现该函数,并且应该作为 C 共享对象的回调调用。我的解决方案是在最后一步抛出分段错误。
//////////////in libcshared.so ///////////
typedef void (*test_function_ptr)(int a)
void callerfun(test_function_ptr callback)
//////////////in GO program//////C program
void* gofunptr;
void implfun(int x){
GOSharedfn(gofunptr);
}
-------------
func main(){
C.gofunptr = &gofun
var callback = C.test_function_ptr(unsafe.Pointer(C.implfun))
C.callerfun(callback )
}
var gofun = goimpl
func goimpl(){
}
/////////GO shared lib libgoshared.so/////////<br>
func GOSharedfn(unsafe.Pointer pointer){
f:=(cast to fun ptr)pointer
f();//Segmentation fault
}
解决方案
推荐阅读
- node.js - firebase 服务后如何访问功能
- flutter - Flutter 版本管理。错误:-bash:fvm:找不到命令
- python - Python Lambda:删除快照但按标签排除
- c# - 当此剃须刀组件从其他 Razor 组件放置在 MainLayout 中时,如何显示放置在 razor 组件中的对话框?
- r - 计算具有交互作用的混合模型中斜率的边际效应(分类和度量协变量之间)
- php - 在 Laravel Query Builder 中向 eloquent 添加自定义集合
- java - java.lang.ClassNotFoundException: com.eclipsesource.json.JsonObject
- bazel - Bazel如何为依赖项添加前缀?
- c# - 仅查找现有记录
- python - 熊猫数据框到元组列表的字典