教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang 生成 shared object 供其他语言使用

golang 生成 shared object 供其他语言使用

发布时间:2022-12-09   编辑:jiaochengji.com
教程集为您提供golang 生成 shared object 供其他语言使用等资源,欢迎您收藏本站,我们将为您提供最新的golang 生成 shared object 供其他语言使用资源
<h1>golang 生成 shared object 供其他语言使用</h1> <h2>LINUX so 文件基本概念和命名规则</h2>

<span class="img-wrap"></span>

libxmns.so.1.2.3 1 major 2 minor 3 release

<ul><li>major 增加,原有函数接口已经不能使用,minor和release 复归于0</li> <li>minor 增加, 新增加了一些函数接口,但原有函数接口还能使用, release 复归于0</li> <li>release 增加,修改一些bug, 函数接口不变</li> </ul><h1>c-go</h1> <h2>模板-供c、java等编译型语言或脚本语言使用</h2> <pre><code class="go">package main import "C" import "fmt" //export Sum func Sum(a int, b int) int { return a b } //export GetName func GetName(firstName string) string{ return fmt.Sprint(firstName,"-so") } func main(){ }</code></pre> <blockquote>注意,即使是要编译成动态库,也要有main函数,上面的import "C"一定要有 而且一定要有注释</blockquote> <h2>编译</h2> <pre><code class="bash">go build -buildmode=c-shared -o libhello.so .\libhello.go</code></pre> <h2>使用lua脚本语言调用</h2> <blockquote>使用到的库: lua2go </blockquote> <h3>luajit 环境变量配置</h3> <pre><code class="bash"> export LUA_PATH="~/?.lua;;" export LUAJIT_LIB=/usr/local/openresty/luajit/lib export LUAJIT_INC=/usr/local/openresty/luajit/include/luajit-2.1 export LUAJIT_HOME=/usr/local/openresty/luajit PATH=$PATH:$LUAJIT_HOME/bin export PATH</code></pre> <h3>调用demo</h3> <pre><code class="lua"> local lua2go = require('lua2go') local example = lua2go.Load('./libvibrant.so') lua2go.Externs[[ extern GoInt32 Sum(GoInt32 a,GoInt32 b); ]] print(example.Sum(1,100))</code></pre> <h3>调用测试</h3> <pre><code class="bash">luajit test_go.lua</code></pre> <h1>plug 模式</h1> <blockquote>1、golang 1.8 支持
2、只支持 golang</blockquote> <h2>模板</h2> <pre><code class="go">package main import ( "fmt" ) func DCall(){ fmt.Println("plugin.so was called") } func DCallWithParam(msg string){ fmt.Println("参数内容为:",msg) } func main() { fmt.Println("goroute全部退出") }</code></pre> <h2>编译</h2> <pre><code class="bash">go build --buildmode=plugin plugin.go </code></pre> <h2>使用</h2> <pre><code class="go">package main import ( "plugin" ) func main() { //加载动态库 p, err := plugin.Open("plugin.so") if err != nil { panic(err) } //查找函数 f, err := p.Lookup("DCall") if err != nil { panic(err) } //转换类型后调用函数 f.(func())() f2, err := p.Lookup("DCallWithParam") if err != nil { panic(err) } //带参函数的调用 f2.(func(string))("hello world,plugin.so") }</code></pre> <h1>go buildmode 说明</h1> <pre><code class="bash">The 'go build' and 'go install' commands take a -buildmode argument which indicates which kind of object file is to be built. Currently supported values are: -buildmode=archive Build the listed non-main packages into .a files. Packages named main are ignored. -buildmode=c-archive Build the listed main package, plus all packages it imports, into a C archive file. The only callable symbols will be those functions exported using a cgo //export comment. Requires exactly one main package to be listed. -buildmode=c-shared Build the listed main package, plus all packages it imports, into a C shared library. The only callable symbols will be those functions exported using a cgo //export comment. Requires exactly one main package to be listed. -buildmode=default Listed main packages are built into executables and listed non-main packages are built into .a files (the default behavior). -buildmode=shared Combine all the listed non-main packages into a single shared library that will be used when building with the -linkshared option. Packages named main are ignored. -buildmode=exe Build the listed main packages and everything they import into executables. Packages not named main are ignored. -buildmode=pie Build the listed main packages and everything they import into position independent executables (PIE). Packages not named main are ignored. -buildmode=plugin Build the listed main packages, plus all packages that they import, into a Go plugin. Packages not named main are ignored.</code></pre> 到此这篇关于“golang 生成 shared object 供其他语言使用”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
golang 生成 shared object 供其他语言使用
想系统学习GO语言(Golang
golang 动态生成函数_GoLang的优点和缺点
提速 72 倍,在 Python 里面调用 Golang 函数
libmysqlclient.so.15()(64bit) is needed by perl-DBD-MySQL-3.0007-2.el5.x86_64的解决
计算机基础:3、计算机层次与编程语言
Go语言基础 001
golang sync .pool
Go语言的主要特性和发展影响
Go 语言到底适合干什么?

[关闭]
~ ~