7

I have noticed that when using go build the binary result can be in excess of 2MB; but using gccgo the binary is less than 35k.

The other issue that I noticed when using gccgo is that the produced binary isn't runnable on another linux box (missing libgo.so I believe) but the go build binary runs just fine (so I imagine its because the binary includes all that is necessary to run?); is there a way to do this with gccgo?

3 Answers3

8

You have to use the -static flag:

Use the -static option to do a fully static link (the default for the gc compiler).

http://golang.org/doc/install/gccgo

fuz
  • 88,405
  • 25
  • 200
  • 352
mna
  • 22,989
  • 6
  • 46
  • 49
5

You can link libgo statically, while still linking the system libraries dynamically, by using the -static-libgo option. (This applies to gccgo only).

iant
  • 1,179
  • 6
  • 6
0

With go 1.8 or above, if go is built with CGO_ENABLED=1 (default to 1), a native compilation links dynamically. Check this variable by running go env CGO_ENABLED. You can switch to link statically by set the environment variable CGO_ENABLED to 0 when running go build.

CGO_ENABLED=0 go build

BTW, cross compiling uses static linkage automatically.

Adil B
  • 14,635
  • 11
  • 60
  • 78
samsong8610
  • 137
  • 1
  • 7