Go程序未使用1.4.2进行静态链接。

huangapple go评论71阅读模式
英文:

Go program not statically linked using 1.4.2

问题

尝试构建一个静态链接版本的Go程序,该程序运行一个HTTP服务器,并使用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时无效。构建成功,但程序没有静态链接。

有人知道发生了什么吗?

英文:

Trying to build a statically linked version of a go program that runs an http server, and uses the net package to determine and parse the IP address of the incoming request. With this build statement:

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

And this preamble in my program:

<!-- language: lang-go -->

package main

import (
    &quot;encoding/json&quot;
    &quot;errors&quot;
    &quot;fmt&quot;
    &quot;log&quot;
    &quot;net&quot;
    &quot;net/http&quot;
    &quot;path/filepath&quot;
    &quot;strings&quot;

    &quot;golang.org/x/blog/content/context/userip&quot;

    &quot;github.com/oschwald/maxminddb-golang&quot;
)

This worked building with go 1.3, producing a statically linked program, but is not working with go 1.4.2. The build succeeds, but the program is not statically linked.

Does anyone know what is going on?

答案1

得分: 5

更多的搜索发现了关于无法静态链接导入net包的程序的问题的这个golang问题跟踪器中的线程,原因是1.3和1.4之间的更改。

往下阅读,我发现ianlancetaylor建议的这个解决方法,使用-installsuffix开关来指定cgo。这使得我的构建语句变为:

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

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

英文:

More searching turned up this thread in the golang issue tracker about being unable to statically link a program that imports the net package due to changes between 1.3 and 1.4.

Reading down I found this workaround suggested by ianlancetaylor to use the -installsuffix switch to specify cgo. This made my build statement:

CGO_ENABLED=0 go install -a -installsuffix cgo -ldflags &#39;-s&#39; .

Which worked for me. Build succeeded and produced a statically linked program.

huangapple
  • 本文由 发表于 2015年6月20日 20:43:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/30954159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定