英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论