【问题标题】:Go program not statically linked using 1.4.2Go 程序未使用 1.4.2 进行静态链接
【发布时间】:2015-06-20 12:43:48
【问题描述】:

尝试构建一个运行http服务器的go程序的静态链接版本,并使用net包来确定和解析传入请求的IP地址。使用此构建语句:

CGO_ENABLED=0 go install -a -ldflags '-s' .

我的程序中的这个序言:

package main

import (
    "encoding/json"
    "errors"
    "fmt"
    "log"
    "net"
    "net/http"
    "path/filepath"
    "strings"

    "golang.org/x/blog/content/context/userip"

    "github.com/oschwald/maxminddb-golang"
)

这适用于 go 1.3 构建,生成静态链接程序,但不适用于 go 1.4.2。构建成功,但程序没有静态链接。

有人知道怎么回事吗?

【问题讨论】:

    标签: go static-linking


    【解决方案1】:

    更多搜索出现在this thread in the golang issue tracker,关于由于 1.3 和 1.4 之间的更改而无法静态链接导入 net 包的程序。

    向下阅读我发现this workaround suggested by ianlancetaylor 使用-installsuffix 开关来指定cgo。这使我的构建声明:

    CGO_ENABLED=0 go install -a -installsuffix cgo -ldflags '-s' .
    

    这对我有用。构建成功并生成了一个静态链接的程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 2016-04-22
      • 2010-09-26
      • 2023-03-19
      • 2013-12-13
      相关资源
      最近更新 更多