C/C++调用Golang 二

《C/C++调用Golang 一》简单介绍了C/C++调用Golang的方法步骤,只涉及一个简单的函数调用。本文总结具体项目中的使用场景,将介绍三种较复杂的调用方式:一,C++向golang传入复杂结构体;二,C++向golang传入回调函数,在golang中调用C++函数;三,C++调用golang函数,返回复杂的结构体。

(本文后面涉及三个例子,省略了编译步骤,仅展示关键代码。具体操作步骤参考《C/C++调用Golang 一》)

一 C++向golang传入复杂结构体

采用avro来序列化与反序列化结构体。C++版avro使用官方版本,golang版avro使用gopkg.in/alanctgardner/gogen-avro.v4 。(C++代码省略了avro结构体的序列化与反序列化,仅展示C++与Golang的交互部分)

1.1 Golang 代码

package main

import "C"

import "fmt"

//export WriteData

func WriteData(data []byte) int {

fmt.Println("WriteData ", data, len(data))

return 0

}

func main() {

}

编译生成的头文件

/* Created by "go tool cgo" - DO NOT EDIT. */

/* package c_references_to_go/sample1 */

/* Start of preamble from import "C" comments.  */

/* End of preamble from import "C" comments.  */

/* Start of boilerplate cgo prologue.  */

#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H

#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;

typedef unsigned char GoUint8;

typedef short GoInt16;

typedef unsigned short GoUint16;

typedef int GoInt32;

typedef unsigned int GoUint32;

typedef long long GoInt64;

typedef unsigned long long GoUint64;

typedef GoInt32 GoInt;

typedef GoUint32 GoUint;

//typedef __SIZE_TYPE__ GoUintptr;

typedef float GoFloat32;

typedef double GoFloat64;

//typedef float _Complex GoComplex64;

//typedef double _Complex GoComplex128;

/*

static assertion to make sure the file is being used on architecture

at least with matching size of GoInt.

*/

typedef char _check_for_32_bit_pointer_matching_GoInt[sizeof(void*)==32/8 ? 1:-1];

typedef struct { const char *p; GoInt n; } GoString;

typedef void *GoMap;

typedef void *GoChan;

typedef struct { void *t; void *v; } GoInterface;

typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus

extern "C" {

#endif

extern GoInt WriteData(GoSlice p0);

#ifdef __cplusplus

}

#endif

1.2 C++代码

#include <Windows.h>

#include <stdio.h>

#include "sample1.h"

//#include "LargeStruct.h"

typedef GoInt (*funcPtrWriteData)(GoSlice p0);

int main(){

HMODULE h = LoadLibraryA("sample1.dll");

if (NULL == h || INVALID_HANDLE_VALUE == h)

{

return -1;

}

funcPtrWriteData pfWriteData = (funcPtrWriteData)GetProcAddress(h,"WriteData");

if (pfWriteData)

{

/* LargeStruct ls;

ls.ID = "100001";

ls.Name = "Peter";

Pet pet;

pet.Type = "Dog";

pet.Name = "WangCai";

pet.Age = 5;

ls.Pets.push_back(pet);*/

GoSlice p0;

p0.data = 0;  //serial ls to binary

p0.len = p0.cap = 0;  //binary len

pfWriteData(p0);

}

FreeLibrary(h);

return 0;

}

二 C++向golang传入回调函数

2.1 Golang 代码

设置回调需要中间的桥接函数 CReportData

package main

import (

"fmt"

)

/*

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef int (*ptfFuncReportData)(const char* data,int len);

extern int CReportData(ptfFuncReportData pf,const char* data,int len);

*/

import "C"

import (

"bytes"

"c_references_to_go/sample3/avro_struct"

"unsafe"

)

var callBackFunc C.ptfFuncReportData

//export SetCallBack

func SetCallBack(f C.ptfFuncReportData) {

callBackFunc = f

}

//export BeginWork

func BeginWork() {

go func() {

for index := 0; index < 10; index++ {

var ls avro_struct.LargeStruct

ls.ID = fmt.Sprintf("ID%d", 1000+index)

ls.Name = fmt.Sprintf("Peter%d", index)

ls.Pets = []*avro_struct.Pet{&avro_struct.Pet{Type: "Dog", Name: "WangCai", Age: 5}}

var buf bytes.Buffer

ls.Serialize(&buf)

dataSlice := buf.Bytes()

GoReportData(dataSlice)

}

}()

}

func GoReportData(data []byte) {

C.CReportData(callBackFunc, (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)))

}

func main() {

}

bridge.c

#include "_cgo_export.h"

int CReportData(ptfFuncReportData pf,const char* data,int len){

return pf(data,len);

}

编译后产生的头文件

/* Created by "go tool cgo" - DO NOT EDIT. */

/* package c_references_to_go/sample2 */

/* Start of preamble from import "C" comments. */

#line 7 "Y:\\mygo\\src\\c_references_to_go\\sample2\\main.go"

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef int (*ptfFuncReportData)(const char* data,int len);

extern int CReportData(ptfFuncReportData pf,const char* data,int len);

#line 1 "cgo-generated-wrapper"

/* End of preamble from import "C" comments. */

/* Start of boilerplate cgo prologue. */

#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H

#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;

typedef unsigned char GoUint8;

typedef short GoInt16;

typedef unsigned short GoUint16;

typedef int GoInt32;

typedef unsigned int GoUint32;

typedef long long GoInt64;

typedef unsigned long long GoUint64;

typedef GoInt32 GoInt;

typedef GoUint32 GoUint;

typedef __SIZE_TYPE__ GoUintptr;

typedef float GoFloat32;

typedef double GoFloat64;

typedef float _Complex GoComplex64;

typedef double _Complex GoComplex128;

/*

static assertion to make sure the file is being used on architecture

at least with matching size of GoInt.

*/

typedef char _check_for_32_bit_pointer_matching_GoInt[sizeof(void*)==32/8 ? 1:-1];

typedef struct { const char *p; GoInt n; } GoString;

typedef void *GoMap;

typedef void *GoChan;

typedef struct { void *t; void *v; } GoInterface;

typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue. */

#ifdef __cplusplus

extern "C" {

#endif

extern void SetCallBack(ptfFuncReportData p0);

extern void BeginWork();

#ifdef __cplusplus

}

#endif

2.2 C++代码

#include <Windows.h>

#include <stdio.h>

#include "sample2.h"

typedef void (*funcPtrSetCallBack)(ptfFuncReportData p0);

typedef void (*funcPtrBeginWork)();

int OnReportData(const char* data,int len){

printf("OnReportData %x %d\r\n",data,len);

return 0;

}

int main(){

HMODULE h = LoadLibraryA("sample2.dll");

if (NULL == h || INVALID_HANDLE_VALUE == h)

{

return -1;

}

funcPtrSetCallBack pfSetCallBack = (funcPtrSetCallBack)GetProcAddress(h,"SetCallBack");

funcPtrBeginWork pfBeginWork = (funcPtrBeginWork)GetProcAddress(h,"BeginWork");

if (pfSetCallBack)

{

pfSetCallBack(OnReportData);

}

if (pfBeginWork)

{

pfBeginWork();

}

Sleep(1000*10);

FreeLibrary(h);

return 0;

}

运行之后的输出:

三 C++调用golang函数返回复杂结构体

不能向C++程序返回Go slice、Go struct。(详情见master分支 src/cmd/cgo/doc.go ,参考5)

3.1 Golang代码

package main

import (

"bytes"

"unsafe"

)

/*

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct {

char* Data;

int DataLen;

} GetLargeStruct_return;

extern GetLargeStruct_return* CopyLargeSturct(char* data,int dataLen);

extern void FreeLargeSturct(GetLargeStruct_return* ptr);

*/

import "C"

import (

"c_references_to_go/sample3/avro_struct"

)

//export GetLargeStruct

func GetLargeStruct(paraIn int) *C.GetLargeStruct_return {

var ls avro_struct.LargeStruct

ls.ID = "1000001"

ls.Name = "Peter"

ls.Pets = []*avro_struct.Pet{&avro_struct.Pet{Type: "Dog", Name: "WangCai", Age: 5}}

var buf bytes.Buffer

ls.Serialize(&buf)

dataSlice := buf.Bytes()

return C.CopyLargeSturct((*C.char)(unsafe.Pointer(&dataSlice[0])), C.int(len(dataSlice)))

}

//export FreeLargeStruct

func FreeLargeStruct(ptr *C.GetLargeStruct_return) {

C.FreeLargeSturct(ptr)

}

//export GetLargeStruct2

func GetLargeStruct2(paraIn int) (*C.char, int) {

var ls avro_struct.LargeStruct

ls.ID = "1000001"

ls.Name = "Peter"

ls.Pets = []*avro_struct.Pet{&avro_struct.Pet{Type: "Dog", Name: "WangCai", Age: 5}}

var buf bytes.Buffer

ls.Serialize(&buf)

dataSlice := buf.Bytes()

return (*C.char)(unsafe.Pointer(C.CBytes(dataSlice))), len(dataSlice)

}

//export FreeCBytes

func FreeCBytes(ptr *C.char) {

C.free(unsafe.Pointer(ptr))

}

func main() {

}

C函数源码文件 (释放C分配的内存)

#include "_cgo_export.h"

GetLargeStruct_return* CopyLargeSturct(char* data,int dataLen){

GetLargeStruct_return* result = (GetLargeStruct_return*)malloc(sizeof(GetLargeStruct_return));

result->DataLen = dataLen;

result->Data = 0;

if(dataLen>0){

result->Data = malloc(dataLen);

memcpy(result->Data,data,dataLen);

}

return result;

}

void FreeLargeSturct(GetLargeStruct_return* ptr){

if(ptr != 0){

if(ptr->Data != 0 ){

free(ptr->Data);

}

free(ptr);

}

}

编译后产生的头文件

/* Created by "go tool cgo" - DO NOT EDIT. */

/* package c_references_to_go/sample3 */

/* Start of preamble from import "C" comments. */

#line 8 "Y:\\mygo\\src\\c_references_to_go\\sample3\\main.go"

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct {

char* Data;

int DataLen;

} GetLargeStruct_return;

extern GetLargeStruct_return* CopyLargeSturct(char* data,int dataLen);

extern void FreeLargeSturct(GetLargeStruct_return* ptr);

#line 1 "cgo-generated-wrapper"

/* End of preamble from import "C" comments. */

/* Start of boilerplate cgo prologue. */

#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H

#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;

typedef unsigned char GoUint8;

typedef short GoInt16;

typedef unsigned short GoUint16;

typedef int GoInt32;

typedef unsigned int GoUint32;

typedef long long GoInt64;

typedef unsigned long long GoUint64;

typedef GoInt32 GoInt;

typedef GoUint32 GoUint;

typedef __SIZE_TYPE__ GoUintptr;

typedef float GoFloat32;

typedef double GoFloat64;

typedef float _Complex GoComplex64;

typedef double _Complex GoComplex128;

/*

static assertion to make sure the file is being used on architecture

at least with matching size of GoInt.

*/

typedef char _check_for_32_bit_pointer_matching_GoInt[sizeof(void*)==32/8 ? 1:-1];

typedef struct { const char *p; GoInt n; } GoString;

typedef void *GoMap;

typedef void *GoChan;

typedef struct { void *t; void *v; } GoInterface;

typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue. */

#ifdef __cplusplus

extern "C" {

#endif

extern GetLargeStruct_return* GetLargeStruct(GoInt p0);

extern void FreeLargeStruct(GetLargeStruct_return* p0);

/* Return type for GetLargeStruct2 */

struct GetLargeStruct2_return {

char* r0;

GoInt r1;

};

extern struct GetLargeStruct2_return GetLargeStruct2(GoInt p0);

extern void FreeCBytes(char* p0);

#ifdef __cplusplus

}

#endif

3.2 C++ 代码

#include <Windows.h>

#include <stdio.h>

#include "sample3.h"

typedef GetLargeStruct_return* (*funcPtrGetLargeStruct)(GoInt p0);

typedef void (*funcPtrFreeLargeStruct)(GetLargeStruct_return* p0);

typedef struct GetLargeStruct2_return (*funcPtrGetLargeStruct2)(GoInt p0);

typedef void (*funcPtrFreeCBytes)(char* p0);

int main(){

HMODULE h = LoadLibraryA("sample3.dll");

if (NULL == h || INVALID_HANDLE_VALUE == h)

{

return -1;

}

funcPtrGetLargeStruct pfGetLargeStruct = (funcPtrGetLargeStruct)GetProcAddress(h,"GetLargeStruct");

funcPtrFreeLargeStruct pfFreeLargeStruct = (funcPtrFreeLargeStruct)GetProcAddress(h,"FreeLargeStruct");

if (pfGetLargeStruct)

{

GetLargeStruct_return* result = pfGetLargeStruct(5);

if (result)

{

printf("GetLargeStruct(5) return  %x %d\r\n",result->Data,result->DataLen);

if (pfFreeLargeStruct)

{

pfFreeLargeStruct(result);

}

}

}

funcPtrGetLargeStruct2 pfGetLargeStruct2 = (funcPtrGetLargeStruct2)GetProcAddress(h,"GetLargeStruct2");

funcPtrFreeCBytes pfFreeCBytes = (funcPtrFreeCBytes)GetProcAddress(h,"FreeCBytes");

if (pfGetLargeStruct)

{

GetLargeStruct2_return result = pfGetLargeStruct2(5);

printf("GetLargeStruct2(5) return  %x %d\r\n",result.r0,result.r1);

if (pfFreeCBytes)

{

pfFreeCBytes(result.r0);

}

}

FreeLibrary(h);

return 0;

}

运行之后的输出:

 

本文只讲述C/C++怎么调用golang程序,细节、注意事项及其他在后续随笔中介绍。

参考文献:

  1. C? Go? Cgo!      https://blog.golang.org/c-go-cgo
  2. Command cgo     https://golang.org/cmd/cgo/
  3. Cgo             https://github.com/golang/go/wiki/cgo
  4. cmd/cgo: Go type not supported in export: struct #18412

https://github.com/golang/go/issues/18412

  1. https://go.googlesource.com/go/+/master/src/cmd/cgo/doc.go

C/C++调用Golang 二的更多相关文章

  1. .Net组件程序设计之远程调用(二)

    .Net组件程序设计之远程调用(二) 激活模式 引用封送对象激活类型两种, 一种是客户端激活类型,一种是服务器端激活. 客户端激活对象 客户端激活方式:当客户端创建一个远程对象时,客户端得到的是一个新 ...

  2. C/C++调用Golang 一

    C/C++调用Golang 一 (开发环境: 操作系统: windows 7 32位操作系统 C++: visual studio 2010 Golang:go version go1.9 windo ...

  3. 从源码(编译)安装golang 二

    h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; te ...

  4. 记一次坑爹的golang 二维map判断问题

    记一次坑爹的golang 二维map判断问题 2018年10月18日 23:16:21 yinnnnnnn 阅读数:32更多 个人分类: golang   版权声明:本文为博主原创文章,未经博主允许不 ...

  5. 函数和常用模块【day04】:函数参数及调用(二)

    本节内容 1.为什么要有参数 2.返回值 3.有参数函数调用 一.为什么要有参数? 无参数实现 def CPU报警邮件() #发送邮件提醒 连接邮箱服务器 发送邮件 关闭连接 def 硬盘报警邮件() ...

  6. Android的JNI调用(二)

    Android Studio 2.3在native下已经有了代码提示功能,按照提示下载相应组件就可以debug native代码. 一.Java调用JNI与JNI调用Java 1.1 C调用Java ...

  7. electron/nodejs实现调用golang函数

    https://www.jianshu.com/p/a3be0d206d4c 思路 golang 支持编译成c shared library, 也就是系统中常见的.so(windows下是dll)后缀 ...

  8. Golang 二维切片初始化

    package main import "fmt" func main() { // 方法0 row, column := 3, 4 var answer [][]int for ...

  9. golang 二维切片

    初始化: res := make([][length]int, length), 例如: res := make([][2]int, 10) fmt.Println(res) 输出: [[0 0] [ ...

随机推荐

  1. ios判断手机号是否可用

    + (BOOL)valiMobile:(NSString *)mobileNum { if (mobileNum.length != 11) { return NO; } /** * 手机号码: // ...

  2. thinkphp整合系列之极验滑动验证码

    对于建站的筒子们来说:垃圾广告真是让人深恶痛绝:为了清净:搞个难以识别的验证码吧:又被用户各种吐槽:直到后来出现了极验这个滑动的验证码:这真是一个体验好安全高的方案:官网:http://www.gee ...

  3. Awk,Cat,Head分析Nginx日志常用命令

    Nginx 日志分析   1.根据访问IP统计UV   awk '{print $1}'  access.log|sort | uniq -c |wc -l   2.统计访问URL统计PV   awk ...

  4. jQuery选取所有复选框被选中的值并用Ajax异步提交数据

    昨天和朋友做一个后台管理系统项目的时候涉及到复选框批量操作,如果用submit表单提交挺方便的,但是要实现用jQuery结合Ajax异步提交数据就有点麻烦了,因为我之前做过的项目中基本上没用Ajax来 ...

  5. 游标遍历所有数据库循环执行修改数据库的sql命令

    MSSQL数据库服务器上有很多类似的数据库,需要将这些数据库统一修改其中的某些表或者某些命令,那么就会想到用游标来遍历. 先来说思路: 1,首先需要查询出所有的数据库: select [name] f ...

  6. 独家探寻阿里安全潘多拉实验室,完美越狱苹果iOS11.2.1

    知道如何从攻击的视角去发现漏洞,才能建立更安全的体系,促进了整个生态的良性发展.以阿里安全潘多拉实验室为例,在对移动系统安全研究的过程中,把研究过程中发现的问题上报给厂商,促进系统安全性的提升. 小编 ...

  7. 对象存取器属性:getter和setter

    在一个对象中,操作其中的属性或方法,通常运用最多的就是读(引用)和写了,譬如说o.a,这就是一个读的操作,而o.b = 1则是一个写的操作.事实上在除ie外最新主流浏览器的实现中,任何一个对象的键值都 ...

  8. onunload事件和onbeforeunload事件

    记录知识点背景:在做一个h5项目时,在统计事件时有这样一个需求, 希望能统计到用户是从第几页离开的,用到了这个知识点.在此记录. window.onunload 1.定义和用法 onunload事件在 ...

  9. 谷哥的小弟学前端(11)——JavaScript基础知识(2)

    探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 具体解释Android主流框架不可或缺的基石 站在源代码的肩膀上全解Scroller工作机制 Android多分辨率适 ...

  10. uva 1378 - A Funny Stone Game(组合游戏)

    题目链接:uva 1378 - A Funny Stone Game 题目大意:两个人玩游戏,对于一个序列,轮流操作.每次选中序列中的i,j,k三个位置要求i<j≤k,然后arr[i]减1,对应 ...