英文:
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 (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
"path/filepath"
"strings"
"golang.org/x/blog/content/context/userip"
"github.com/oschwald/maxminddb-golang"
)
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 '-s' .
Which worked for me. Build succeeded and produced a statically linked program.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论