代码之家  ›  专栏  ›  技术社区  ›  Aleksander Alekseev

使用-buildmode=shared和-linkshared标志减小Go中的插件(.so)大小

  •  1
  • Aleksander Alekseev  · 技术社区  · 6 年前

    我想通过删除插件和主程序中常用的符号来减小插件(.so文件)的大小,例如go运行时和标准库。

    这是我的测试项目。

    package main
    
    import (
        "fmt"
        "os"
        "plugin"
    )
    
    type Plugin interface {
        SayHello()
        SayGoodbye()
    }
    
    func main() {
        if len(os.Args) < 2 {
            fmt.Fprintf(os.Stderr, "Usage: %s <plugin.so>\n", os.Args[0])
            os.Exit(1)
        }
    
        p, err := plugin.Open(os.Args[1])
        if err != nil {
            fmt.Fprintf(os.Stderr, "plugin.Open: %v\n", err)
            os.Exit(1)
        }
    
        piTemp, err := p.Lookup("Plugin")
        if err != nil {
            fmt.Fprintf(os.Stderr, "plugin.Open: %v\n", err)
            os.Exit(1)
        }
    
        pi, ok := piTemp.(Plugin)
        if !ok {
            fmt.Fprintf(os.Stderr, "Unable to cast helloTemp to Plugin interface\n")
            os.Exit(1)
        }
    
        pi.SayHello()
        pi.SayGoodbye()
    }
    

    插件/插件.go:

    package main
    
    import "fmt"
    
    type PluginImpl struct {}
    
    var Plugin PluginImpl
    
    func (pi *PluginImpl) SayHello() {
        fmt.Printf("Hello from the plugin!\n")
    }
    
    func (pi *PluginImpl) SayGoodbye() {
        fmt.Printf("Goodbye from the plugin!\n")
    }
    

    建筑.sh:

    #!/bin/sh
    
    set -e
    
    go clean ./...
    go build
    cd plugin
    go build -buildmode=plugin
    

    使用示例:

    $ ./plugins-test ./plugin/plugin.so
    Hello from the plugin!
    Goodbye from the plugin!
    

    我试着从 this article 我得到的是:

    $ go install -buildmode=shared std
    $ go install -buildmode=shared -linkshared go-sandbox/plugins-test
    panic: runtime error: index out of range
    
    goroutine 1 [running]:
    cmd/go/internal/work.(*Builder).linkSharedAction.func2(0x84607e0)
        /usr/local/go/src/cmd/go/internal/work/action.go:745 +0x7dd
    cmd/go/internal/work.(*Builder).cacheAction(0x8d7cfc0, 0x90fdd00, 0x14, 0x0, 0x8efc740, 0x90fdd00)
        /usr/local/go/src/cmd/go/internal/work/action.go:314 +0x84
    cmd/go/internal/work.(*Builder).linkSharedAction(0x8d7cfc0, 0x1, 0x1, 0x8e2373a, 0x6, 0x8eec840, 0x8e2373a)
        /usr/local/go/src/cmd/go/internal/work/action.go:738 +0x201
    cmd/go/internal/work.(*Builder).buildmodeShared(0x8d7cfc0, 0x1, 0x1, 0x8c18050, 0x1, 0x2, 0x0, 0x0, 0x0, 0x8eec840, ...)
        /usr/local/go/src/cmd/go/internal/work/action.go:649 +0xe8
    cmd/go/internal/work.InstallPackages(0x8c18050, 0x1, 0x2, 0x0, 0x0, 0x0)
        /usr/local/go/src/cmd/go/internal/work/build.go:503 +0xb9c
    cmd/go/internal/work.runInstall(0x884ae20, 0x8c18050, 0x1, 0x2)
        /usr/local/go/src/cmd/go/internal/work/build.go:417 +0x52
    main.main()
        /usr/local/go/src/cmd/go/main.go:219 +0x8a4
    

    在上的行为也是一样的 go1.11.2 linux/386 go1.11.2 linux/arm . 在MacOS上,这个功能似乎不见了--- -buildmode=shared not supported on darwin/amd64 .

    0 回复  |  直到 6 年前