当前位置:网站首页>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}}
边栏推荐
- antd组件中a-modal设置固定高度,内容滚动显示
- 锂电池技术
- Using data intelligence, Amazon cloud technology helps companies build endogenous brand growth
- 这一次,话筒给你:向自由软件之父斯托曼 提问啦!
- 奢侈品鉴定机构小程序开发制作功能介绍
- 第三方软件测评有什么作用?权威软件检测机构推荐
- 领域驱动模型设计与微服务架构落地-从项目去剖析领域驱动
- Lack of comparators, op amps come to the rescue!(Op amp is recorded as a comparator circuit)
- 《论文阅读》PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
- 无线网络、HTTP缓存、IPv6
猜你喜欢
随机推荐
win2012安装Oraclerac失败
图式图例规范尺寸
A method that can make large data clustering 2000 times faster
C#实现访问OPC UA服务器
产品使用说明书小程序开发制作说明
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
C#实现访问OPC UA服务器
2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱
Parallels 将扩展桌面平台产品,以进一步改善在 Mac 上运行 Windows 的用户体验和工作效率
Alibaba的秒杀系统—千亿级并发设计手册上线了
缺少比较器,运放来救场!(运放当做比较器电路记录)
TestLink导出用例转换工具
Unfinished mathematics test paper ----- test paper generator (Qt includes source code)
发送post请求前台无法获取数据
易观分析联合中小银行联盟发布海南数字经济指数,敬请期待!
一汽奥迪:持续34年聚焦品质与体验 立足市场需求推进产品迭代
【POI 2008, BLO】Cut Point
In the second half of 2012 system architecture designers afternoon paper II
2012年下半年 系统架构设计师 下午试卷 II
Epoll learn to think: a high performance server processing framework