Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smaller binaries when compiled with static ldflag #18450

Closed
sjpotter opened this issue Dec 28, 2016 · 2 comments
Closed

smaller binaries when compiled with static ldflag #18450

sjpotter opened this issue Dec 28, 2016 · 2 comments

Comments

@sjpotter
Copy link

What version of Go are you using (go version)?

go version go1.6.3 linux/amd64

What operating system and processor architecture are you using (go env)?

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

What did you do?

compiling a project that includes cgo files, hence would normally make a dynamic linked binary (against C runtime) and then compiles it statically.

i.e.

go build ./path/to/command.go

vs

go build --ldflags '-extldflags "-static"' ./path/to/command.go

What did you expect to see?

I expected the dynamic binary to be smaller than the fully statically linked binary.

What did you see instead?

the statically linked binary was significantly smaller, and I'm not the only one to see this

see this from reddit where someone sees similar behavior

https://www.reddit.com/r/golang/comments/46gbdz/binary_sizes_in_go/d058h1u/

@bradfitz
Copy link
Contributor

For questions about Go, see https://golang.org/wiki/Questions.

It looks like you're not comparing dynamic vs static, but dynamic vs static+stripped.

@sjpotter
Copy link
Author

Stripped is even smaller (similar) in both cases.

Full Example

$ go build ./cmd/cmd.go
$ ls -l cmd
-rwxr-xr-x 1 spotter spotter 87322360 Dec 28 11:58 cmd
$ strip cmd
$ ls -l cmd
-rwxr-xr-x 1 spotter spotter 60697704 Dec 28 11:58 cmd

$ go build --ldflags '-extldflags "-static"' ./cmd/cmd.go
$ ls -l cmd
-rwxr-xr-x 1 spotter spotter 70021544 Dec 28 11:59 cmd
$ strip cmd
$ ls -l cmd
-rwxr-xr-x 1 spotter spotter 42909128 Dec 28 12:00 cmd

@golang golang locked and limited conversation to collaborators Dec 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants