CGO,将结构从go传递到c++; 我正在试用CGO,希望用C++来使用CGO。我找到了关于做这件事的帖子。如果我有一个C++结构,名为“代码> Foo< /Cube >,并且一个GO结构名为 Foo,我想把GO Foo传递给C++。我试着这样做: //bind.h #ifdef __cplusplus extern "C" { #endif #include "structs.hpp" void bindCgo(Foo bar); #ifdef __cplusplus } #endif
现在当我运行这个程序时,它会给我CGO,将结构从go传递到c++; 我正在试用CGO,希望用C++来使用CGO。我找到了关于做这件事的帖子。如果我有一个C++结构,名为“代码> Foo< /Cube >,并且一个GO结构名为 Foo,我想把GO Foo传递给C++。我试着这样做: //bind.h #ifdef __cplusplus extern "C" { #endif #include "structs.hpp" void bindCgo(Foo bar); #ifdef __cplusplus } #endif,c++,c,go,struct,cgo,C++,C,Go,Struct,Cgo,现在当我运行这个程序时,它会给我sΘ\。这正常吗?我该如何修复 我的结构中也有贴图和向量,因此使用char*将不起作用似乎永远不会起作用 STD::String 是C++类,GO不与C++一起播放,C.不理解GO STR < /C>类型,只需传递基元类型 //structs.hpp #ifndef STRUCTS_HPP_ #define STRUCTS_HPP_ typedef struct Foo { #ifdef __cplusplus std::string str;
sΘ\
。这正常吗?我该如何修复
我的结构中也有贴图和向量,因此使用char*将不起作用似乎永远不会起作用<代码> STD::String 是C++类,GO不与C++一起播放,C.不理解GO <代码> STR < /C>类型,只需传递基元类型
//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))))
}