当前位置:网站首页>SWIG教程《四》-go语言的封装
SWIG教程《四》-go语言的封装
2022-08-10 14:31:00 【阿酷尔工作室】
以Go 封装为例
go语言不支持直接调用C或者C++语言,虽然通过cgo转接go可以实现对c的调用,但是调用过程各种数据类型之间无法还是无法直接转换,使用起来也不是很方便,而swig刚好能够填补这个空白
而且只要是go 1.2之后的版本,可以通过go build直接使用swig生成的源码
虽然swig会尽量将C、C++封装成go代码,但是由于两种语言之间的不同,还是会有部分改动在里面,比如const类型通过只对外提供Get接口来实现,通过defer来实现对对象内存的管理等。
为了能够实现go->c++->go这种形式的调用,必须借助代理模式,代理模式必须在指令文件中完成一下内容
先在模块上声明使用代理模式
%module(directors="1") modulename
在需要设置代理的类上指定代理
%feature("director") FooBarAbstruct;
导入额外的模块
%go_import("fmt", _ "unusedPackage", rp "renamed/package")
%insert(go_wrapper) %{
func foo() {
fmt.Println("Some string:", rp.GetString())
}
// Importing the same package twice is permitted,
// Go code will be generated with only the first instance of the import.
%go_import("fmt")
%insert(go_wrapper) %{
func bar() {
fmt.Println("Hello world!")
}
%}
使用说明
Swig 给出选项,并指出需要封装的文件,就可以对指定的文件进行封装。
where filename is a SWIG interface file or a C/C++ header file
swig [ options ] filename
swig
Generate bindings between C / C++ code and various high level languages such as Javascript, Python, C#, and more.It uses special .i or .swg files to generate the bindings (C/C++ with SWIG directives, then outputs a C/C++ file that contains all of the wrapper code needed to build an extension module.
- Generate a binding between C++ and Python:
swig -c++ -python -o {{path/to/output_wrapper.cpp}} {{path/to/swig_file.i}}
- Generate a binding between C++ and Go:
swig -go -cgo -intgosize 64 -c++ {{path/to/swig_file.i}}
- Generate a binding between C and Java:
swig -java {{path/to/swig_file.i}}
- Generate a binding between C and Ruby and prefix the Ruby module with {{foo::bar::}}:
swig -ruby -prefix "{{foo::bar::}}" {{path/to/swig_file.i}}
边栏推荐
猜你喜欢
随机推荐
【Gazebo入门教程】第三讲 SDF文件的静/动态编程建模
Lithium battery technology
统信 UOS V20 专业版(1050update2)发布:文件共享、全局搜索等优化
图式图例规范尺寸
这一次,话筒给你:向自由软件之父斯托曼 提问啦!
基于ArcGIS水文分析、HEC-RAS模拟技术在洪水危险性及风险评估
win2012安装Oraclerac失败
自定义picker滚动选择器样式
作业
PHP judges whether the file has content, and if there is no content, copy another file to write
Lack of comparators, op amps come to the rescue!(Op amp is recorded as a comparator circuit)
物资采购小程序开发制作功能介绍
格式化输出当前时间
Error: Rule can only have one resource source (provided resource and test + include + exclude)
leetcode 739. Daily Temperatures 每日温度(中等)
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification
王学岗————直播推流(软便)03x264集成与camera推流
2012年下半年 系统架构设计师 下午试卷 II
【POI 2008, BLO】割点
Pointer (preliminary solution of C language)








