ldflags="-s -w" vs strip

339 views
Skip to first unread message

X Xiao

unread,
May 27, 2020, 1:08:27 PM5/27/20
to golan...@googlegroups.com
what's the difference between `go build -ldflags="-s -w"` and `go build` plus `strip myBinary`? it seems their result is different, sometimes running `strip` gives me smaller size, then why bother with ldflags?

Ian Lance Taylor

unread,
May 27, 2020, 6:59:17 PM5/27/20
to X Xiao, golang-nuts
On Wed, May 27, 2020 at 10:08 AM X Xiao <laos...@gmail.com> wrote:
>
> what's the difference between `go build -ldflags="-s -w"` and `go build` plus `strip myBinary`? it seems their result is different, sometimes running `strip` gives me smaller size, then why bother with ldflags?

I just tried a simple hello, world test and running strip produced a
larger binary (on amd64 GNU/Linux). I think the results are going to
depend on the exact section sizes and alignments. The strip program
is of course a general tool, that knows nothing about Go, that
produces a new copy of the executable.

That said, broadly speaking there shouldn't be any real difference in
the final result. You should use whichever works better for you.

Ian
Reply all
Reply to author
Forward
0 new messages