How to set LDFLAGS globally?

534 views
Skip to first unread message

Ondekoza

unread,
Apr 1, 2014, 11:07:19 AM4/1/14
to golan...@googlegroups.com
I (almost) always compile my tools with -ldflags "-s" to reduce the size of the binaries.

Is there a way to define the -ldflags globally, like in an environment variable or a config file in $GOROOT or as a structured comment in the source?

Thanks
Stefan

PS. Couldn't find anything in the usual places.


minux

unread,
Apr 1, 2014, 11:51:54 AM4/1/14
to Ondekoza, golang-nuts
On Tue, Apr 1, 2014 at 11:07 AM, Ondekoza <onde...@gmail.com> wrote:
I (almost) always compile my tools with -ldflags "-s" to reduce the size of the binaries.
Is there a way to define the -ldflags globally, like in an environment variable or a config file in $GOROOT or as a structured comment in the source?
No, there isn't a way to set it globally (except modifying the source code for cmd/go or cmd/ld or
making your own go command alias/wrapper)

Dave Cheney

unread,
Apr 1, 2014, 3:31:31 PM4/1/14
to golan...@googlegroups.com
-ldflags -s doesn't do what you think it does. Specifically that option was neutered because strip(1)ing go binaries doesn't make them smaller and often leaves them non functional

Gustavo Niemeyer

unread,
Apr 1, 2014, 3:54:00 PM4/1/14
to Dave Cheney, golan...@googlegroups.com
On Tue, Apr 1, 2014 at 4:31 PM, Dave Cheney <da...@cheney.net> wrote:
> -ldflags -s doesn't do what you think it does. Specifically that option was
> neutered because strip(1)ing go binaries doesn't make them smaller and often
> leaves them non functional

It did make the binary significantly smaller whenever I tried it, and
I had a single case where the binary did break. The QML+OpenGL gopher
example goes from 7.7MB to 2.9MB, for example, and spins happily.

That's not to say someone should trust it to work, though. Nobody is
testing this.


gustavo @ http://niemeyer.net

Dave Cheney

unread,
Apr 1, 2014, 3:56:34 PM4/1/14
to Gustavo Niemeyer, golang-nuts
> That's not to say someone should trust it to work, though. Nobody is
> testing this.

Precisely. It can make your binaries smaller, and it can also make
them not work. Stripped binaries are not tested as part of CI or
during the release builds, and bugs previously raised about this have
generally (not always) been closed with a recommendation not to do
that.

Gustavo Niemeyer

unread,
Apr 1, 2014, 4:52:01 PM4/1/14
to Dave Cheney, golang-nuts
Yes, but "because strip(1)ing go binaries doesn't make them smaller"
is not even close to being true. Even a "Hello world" example
including fmt is dramatically reduced in size by strip. Rather than
denying it, we should take that into account for
http://golang.org/issue/6853 (which is a good issue to star for those
interested in binary size matters).
--

gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages