Reputation: 22252
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"
Upvotes: 0
Views: 326
Reputation: 20768
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
Upvotes: 1