如何编译静态链接的 Go 程序(1.3)?

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

How to compile statically linked go program (1.3)?

问题

我正在尝试静态编译一个小的Go程序(为了使用Rocket进行实验)。我在Debian Jessie(Mint版本)上运行。我安装了golang-go软件包。Rocket文档提供了如何为Go版本1.4和1.5进行静态编译的示例。

1.4版本:
$ CGO_ENABLED=0 GOOS=linux go build -o hello -a -installsuffix cgo .

1.5版本:
$ CGO_ENABLED=0 GOOS=linux go build -o hello -a -tags netgo -ldflags '-w' .

不幸的是,go version显示我正在运行1.3版本。

$ go version
go version go1.3.3 linux/amd64

我尝试了1.4版本,希望它能适用于1.3,但没有这样的运气。我不确定是否安装了所有我需要的Debian软件包?

能够使用go build howdy.go编译并运行该文件。这个小应用程序按预期工作,但是ldd显示它有多个动态依赖项:

$ ldd howdy
linux-vdso.so.1 (0x00007ffe72d7e000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3b22e5a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3b22ab1000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3b23077000)

为了完全透明,我尝试静态编译的小程序(howdy.go)如下:

package main

import (
"log"
"net/http"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Printf("request from %v\n", r.RemoteAddr)
w.Write([]byte("howdy\n"))
})
log.Fatal(http.ListenAndServe(":5000", nil))
}

另外,go -x的输出是:

$ go build -x howdy.go
WORK=/tmp/go-build496765737
mkdir -p $WORK/command-line-arguments/_obj/
cd /home/travisg/rkt-v0.10.0
/usr/lib/go/pkg/tool/linux_amd64/6g -o $WORK/command-line-arguments.a -trimpath $WORK -p command-line-arguments -complete -D _/home/travisg/rkt-v0.10.0 -I $WORK -pack ./howdy.go
cd .
/usr/lib/go/pkg/tool/linux_amd64/6l -o howdy -L $WORK -extld=gcc $WORK/command-line-arguments.a

以及go env的输出是:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

英文:

I'm trying to compile a small go program statically (for the purpose of playing with Rocket). I'm running on Debian Jessie (Mint version). I installed the golang-go package. The Rocket documentation gives examples of how to compile statically for go version 1.4 and 1.5

1.4
$ CGO_ENABLED=0 GOOS=linux go build -o hello -a -installsuffix cgo .

1.5:
$ CGO_ENABLED=0 GOOS=linux go build -o hello -a -tags netgo -ldflags '-w' .

Unfortunately, go version says I'm running 1.3.

$ go version
go version go1.3.3 linux/amd64

I tried the 1.4 version, hoping it would for for 1.3, but no such luck. I'm not sure if I installed all the debian packages I even needed?

I was able to compile the file and run it using just go build howdy.go. The small app works as expected, but ldd shows it has multiple dynamic dependencies:

$ ldd howdy
	linux-vdso.so.1 (0x00007ffe72d7e000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f3b22e5a000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3b22ab1000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f3b23077000)

For complete disclosure, the small program I'm trying to compile statically (howdy.go) is:

package main

import (
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        log.Printf("request from %v\n", r.RemoteAddr)
        w.Write([]byte("howdy\n"))
    })
    log.Fatal(http.ListenAndServe(":5000", nil))
}

Additionally, output of go -x is:

$ go build -x howdy.go
WORK=/tmp/go-build496765737
mkdir -p $WORK/command-line-arguments/_obj/
cd /home/travisg/rkt-v0.10.0
/usr/lib/go/pkg/tool/linux_amd64/6g -o $WORK/command-line-arguments.a -trimpath $WORK -p command-line-arguments -complete -D _/home/travisg/rkt-v0.10.0 -I $WORK -pack ./howdy.go
cd .
/usr/lib/go/pkg/tool/linux_amd64/6l -o howdy -L $WORK -extld=gcc $WORK/command-line-arguments.a

and output of go env is:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

答案1

得分: 1

这是对我有效的方法:

CGO_ENABLED=0 \
go build -a -installsuffix cgo -ldflags '-s' -o server server.go

来源:

https://github.com/golang/go/issues/9344

英文:

this is what worked for me:

CGO_ENABLED=0 \
go build -a -installsuffix cgo -ldflags '-s' -o server server.go

from:

https://github.com/golang/go/issues/9344

huangapple
  • 本文由 发表于 2015年11月10日 02:49:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/33616064.html
匿名

发表评论

匿名网友

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

确定