golang generates shared object s for use in other languages

Keywords: Linux Java

Basic concepts and naming rules of LINUX so files

libxmns.so.1.2.3 1 major 2 minor 3 release

  • With the increase of major, the original function interface can no longer be used, minor and release return to 0.
  • minor is increased, some new function interfaces are added, but the original function interfaces can be used, release returns to 0
  • release adds, modifies some bug s, and the function interface remains unchanged

c-go

Template - for use in compiled or scripting languages such as c and 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(){

}

Note that even to compile into dynamic libraries, there must be main functions. The import "C" above must have and must have annotations.

Compile

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

Call using lua scripting language

Used libraries: lua2go

luajit environment variable configuration

  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

Call 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))

Call test

luajit test_go.lua

plug mode

1. golang 1.8 + support
2. Supporting golang only

Template

package main

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

func DCallWithParam(msg string){
    fmt.Println("The parameters are:",msg) 
}


func main() {
    fmt.Println("goroute Quit all")
}

Compile

go build --buildmode=plugin plugin.go 

Use

package main

import (
    "plugin"
)
func main() {

    //Loading dynamic libraries
    p, err := plugin.Open("plugin.so")
    if err != nil {
        panic(err)
    }
    //Lookup function   
    f, err := p.Lookup("DCall")
    if err != nil {
        panic(err)
    }
    //Call a function after converting a type   
    f.(func())()

    f2, err := p.Lookup("DCallWithParam")
    if err != nil {
        panic(err)
    }

    //Call of Functions with Parameters
    f2.(func(string))("hello world,plugin.so")
}

Go build mode description

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.

Posted by waradmin on Fri, 29 Mar 2019 11:06:29 -0700