【发布时间】:2021-09-13 00:13:12
【问题描述】:
我正在尝试从 C++ 到 golang 获取一系列 tensorflow 框预测,但无论我做什么我都无法做到。我有一个 GO 程序,它调用一个函数,该函数使用 cgo 在 C++ 中进行张量流检测。这一切都有效,我能够在 C++ 中得到预测。问题是将这些预测作为一个包含 100 个结构的数组转移到 GO 中,每个结构都包含一个预测。
我可以在 GO 中设置一个指针,并使用这个指针地址在 C++ 中设置一个结构。代码如下。
我想在 C++ 中设置一个结构数组并在 GO 中检索这个数组。我认为使用与之前相同的指针地址并将其用作我的 C++ 数组的地址应该很容易。然后我可以从 GO 中的指针恢复结构。有人对此有解决方案吗?
去
type PredictResult struct {
Loc [4]float32
Score int
Label int
}
var predictions PredictResult
predictions_ptr := unsafe.Pointer(&predictions)
C.LIB_predict(predictions_ptr)
fmt.Println("GO predictions; ", predictions)
bridge.hpp
struct PredictResult{
float Loc[4];
int64_t Score;
int64_t Label;
};
void LIB_predict(void* predictions);
bridge.cpp
void LIB_predict(void* predictions){
PredictResult *p = (PredictResult*)predictions;
p->Score = 6;
p->Label = 95;
}
打印:
GO predictions; {[0 0 0 0] 6 95}
【问题讨论】: