c++ - CGO,将结构从 go 传递到 c++
问题描述
我正在尝试使用 cgo,并希望将 c++ 与 cgo 一起使用。我发现这篇文章是关于这样做的。如果我有一个名为的 c++ 结构Foo
和一个名为的 go 结构Foo
,我想将 go 传递Foo
给 c++。我试过这样做:
//bind.h
#ifdef __cplusplus
extern "C" {
#endif
#include "structs.hpp"
void bindCgo(Foo bar);
#ifdef __cplusplus
}
#endif
//structs.hpp
#ifndef STRUCTS_HPP_
#define STRUCTS_HPP_
typedef struct Foo {
#ifdef __cplusplus
std::string str;
#endif
}
#endif
//bind.cc
#include "structs.hpp"
using namespace std;
void bindCgo(Foo bar) {
cout << bar.str << endl; //this gives "sΘ\"
}
//main.go
import "unsafe"
// #cgo CFLAGS: -std=c99
// #include "bind.h"
import "C"
type Foo struct {
str string
}
func main() {
bar := Foo{""};
C.bindCgo(((*C.Foo)(unsafe.Pointer(&bar))))
}
现在当我运行这个程序时,它给了我sΘ\
. 这是正常的,我该如何解决这个问题?
我的结构中也有地图和向量,因此使用 char* 将不起作用
解决方案
推荐阅读
- c++ - android和windows应用程序之间的TCP Socket连接
- arrays - BASH 数组的单个下标中是否可以有多个值?
- lua - 如何在另一个表中的另一个表中插入另一个表?
- c# - C#总结列表中的多个项目
- android - 来自数据库的 LiveData 不会触发 Transformations.switchMap
- karate - 上传文件时出错","错误":["分配前引用的局部变量 'upload_file_path'" - 在空手道中使用 ".yaml" 文件的发布方法
- xml - 如何为此 XML 文件编写 XSLT 文件
- python - 如何使用 python 更新 Elastic Search ( Kibana ) 中可用的大量 Json 数据
- apache-karaf - ClassCastException:com.sun.crypto.provider.AESCipher$General 无法在 Karaf 4.2.5 上转换为 javax.crypto.CipherSpi
- r - 使用 Apply 来避免“条件的长度 > 1 并且只使用第一个元素”?