当前位置:网站首页>SWIG tutorial "four" - package of go language
SWIG tutorial "four" - package of go language
2022-08-10 14:59:00 【Akure Studio】
以Go 封装为例
goThe language does not support direct invocationC或者C++语言,虽然通过cgo转接go可以实现对c的调用,However, there is no or no direct conversion between the various data types of the calling process,使用起来也不是很方便,而swigJust enough to fill this gap
而且只要是go 1.2之后的版本,可以通过go build直接使用swig生成的源码
虽然swig会尽量将C、C++封装成go代码,But due to the difference between the two languages,There will still be some changes in there,比如constTypes are only available externallyGet接口来实现,通过deferTo realize the management of object memory, etc.
为了能够实现go->c++->gocall of this form,Proxy mode must be used,The proxy mode must complete the following content in the directive file
First declare the use of proxy mode on the module
%module(directors="1") modulename
Specify the proxy on the class where the proxy needs to be set
%feature("director") FooBarAbstruct;
Import additional modules
%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 给出选项,And indicate the files that need to be encapsulated,The specified file can be encapsulated.
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}}
边栏推荐
- 文件系统设计
- 2012年下半年 系统架构设计师 下午试卷 II
- Zhaoqi Technology Innovation High-level Talent Entrepreneurship Competition Platform
- 数字藏品平台系统开发实战
- antd组件中a-modal设置固定高度,内容滚动显示
- fatal error C1083 Unable to open include file 'io.h' No such file
- 【吴恩达来信】强化学习的发展!
- PyTorch multi-machine multi-card training: DDP combat and skills
- leetcode 739. Daily Temperatures Daily Temperatures (Moderate)
- Pagoda panel open Redis to specify the network machine
猜你喜欢
随机推荐
字节终面:CPU 是如何读写内存的?
[Semantic Segmentation] DeepLab Series
leetcode 739. Daily Temperatures 每日温度(中等)
中学数学建模书籍及相关的视频等(2022.08.09)
WSL 提示音关闭
High-paid programmers & interview questions series 135 How do you understand distributed?Do you know CAP theory?
pytest框架优化
Zhaoqi Technology Innovation High-level Talent Entrepreneurship Competition Platform
Summary of Force Buckle Solution 640 - Solving Equations
Mini Program-Voice broadcast function
DB2查询2个时间段之间的所有月份,DB2查询2个时间段之间的所有日期
Understanding_Data_Types_in_Go
mysql进阶(三十三)MySQL数据表添加字段
基于 Azuki 系列:NFT估值分析框架“DRIC”
从洞察到决策,一文解读标签画像体系建设方法论
【MindSpore易点通机器人-02】设计与技术选型
PyTorch multi-machine multi-card training: DDP combat and skills
正则表达式(包含各种括号,echo,正则三剑客以及各种正则工具)
Based on Azuki Series: NFT Valuation Analysis Framework "DRIC"
@RequestBody的使用[通俗易懂]