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

栏目: Lua · 发布时间: 5年前

内容简介:注意,即使是要编译成动态库,也要有main函数,上面的import "C"一定要有 而且一定要有注释使用到的库:

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

LINUX so 文件基本概念和命名规则

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

libxmns.so.1.2.3 1 major 2 minor 3 release

  • major 增加,原有函数接口已经不能使用,minor和release 复归于0
  • minor 增加, 新增加了一些函数接口,但原有函数接口还能使用, release 复归于0
  • release 增加,修改一些bug, 函数接口不变

c-go

模板-供c、 java 等编译型语言或脚本语言使用

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(){

}

注意,即使是要编译成动态库,也要有main函数,上面的import "C"一定要有 而且一定要有注释

编译

go build -buildmode=c-shared -o libhello.so .\libhello.go

使用 lua 脚本语言调用

使用到的库: lua2go

luajit 环境变量配置

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

调用demo

local lua2go = require('lua2go')
 local example = lua2go.Load('./libvibrant.so')
 
 lua2go.Externs[[
   extern GoInt32 Sum(GoInt32 a,GoInt32 b);
 ]]
 
 print(example.Sum(1,100))

调用测试

luajit test_go.lua

plug 模式

1、golang 1.8+ 支持

模板

package main

import (
    "fmt"
    )
func DCall(){
    fmt.Println("plugin.so was called") 
}

func DCallWithParam(msg string){
    fmt.Println("参数内容为:",msg) 
}


func main() {
    fmt.Println("goroute全部退出")
}

编译

go build --buildmode=plugin plugin.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")
}

go buildmode 说明

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.

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Python语言程序设计

Python语言程序设计

(美)Y. Daniel Liang / 机械工业出版社 / 2013-3 / 79.00元

本书保持了Liang博士系列丛书中一贯的、标志性的教与学的哲学:以实例教,由实践学。书中采用了他所提出的已经经过实践检验的“基础先行”的方法,即在定义类之前,首先使用清晰简明的语言介绍基本程序设计概念,如选择语句、循环和函数;在介绍面向对象程序设计和GUI编程之前,首先介绍基本逻辑和程序设计概念。书中除了给出一些以游戏和数学为主的典型实例外,还在每章的开始使用简单的图形给出一两个例子,以激发学生的......一起来看看 《Python语言程序设计》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码