当前位置:网站首页>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}}
边栏推荐
- Unfinished mathematics test paper ----- test paper generator (Qt includes source code)
- 从洞察到决策,一文解读标签画像体系建设方法论
- leetcode 739. Daily Temperatures Daily Temperatures (Moderate)
- Flask框架——MongoEngine使用MongoDB数据库
- awk的简单使用
- 格式化输出当前时间
- 高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
- Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
- Error: Rule can only have one resource source (provided resource and test + include + exclude)
- Classifying irises using decision trees
猜你喜欢
随机推荐
机器学习总结(一)
Flask框架——MongoEngine使用MongoDB数据库
公网IP和内网IP的区别[通俗易懂]
d为何用模板参数
1004(树状数组+离线操作+离散化)
电脑重装系统提示activex部件不能创建对象如何解决
NAACL 2022 | 简单且高效!随机中间层映射指导的知识蒸馏方法
laravel 抛错给钉钉
统信 UOS V20 专业版(1050update2)发布:文件共享、全局搜索等优化
【剑指offer】---数组中的重复数字
PAT甲级 1014 排队等候(队列大模拟+格式化时间)
PHP judges whether the file has content, and if there is no content, copy another file to write
Classifying irises using decision trees
静态变量存储在哪个区
符合信创要求的堡垒机有哪些?支持哪些系统?
MySQL advanced (thirty-three) MySQL data table adding fields
发送post请求前台无法获取数据
How is the monthly salary table stored in the database?Ask for a design idea
WSL 提示音关闭
基于ArcGIS水文分析、HEC-RAS模拟技术在洪水危险性及风险评估









