<p>Let's take kubernetes's kubelet that has a bunch of cgo in things it depends on (or even in itself). I assume this is something that either a) I don't understand about Go b) I am doing wrong c) both.</p>
<p>results show compiled and then stripped size</p>
<p>With Go 1.6 normally I see</p>
<pre><code>$ go build ./cmd/kubelet/kubelet.go ; ls -l kubelet ; strip kubelet ; ls -l kubelet
-rwxr-xr-x 1 compsciphd compsciphd 170777248 Jan 3 15:02 kubelet
-rwxr-xr-x 1 compsciphd compsciphd 120943000 Jan 3 15:02 kubelet
</code></pre>
<p>and if I statically link it via ldflag options, I see</p>
<pre><code>$ go build --ldflags '-extldflags "-static"' ./cmd/kubelet/kubelet.go ; ls -l kubelet ; strip kubelet ; ls -l kubelet
-rwxr-xr-x 1 compsciphd compsciphd 133684544 Jan 3 15:06 kubelet
-rwxr-xr-x 1 compsciphd compsciphd 83362544 Jan 3 15:06 kubelet
</code></pre>
<p>And if I do the same with Go 1.7, I see similar results (though smaller binaries overall as to be expected)</p>
<p>with normal go build</p>
<pre><code>$ /usr/lib/go-1.7/bin/go build ./cmd/kubelet/kubelet.go ; ls -l kubelet ; strip kubelet ; ls -l kubelet
-rwxr-xr-x 1 compsciphd compsciphd 119859816 Jan 3 15:08 kubelet
-rwxr-xr-x 1 compsciphd compsciphd 72698056 Jan 3 15:08 kubelet
</code></pre>
<p>with ldflags to link statically</p>
<pre><code>$ /usr/lib/go-1.7/bin/go build --ldflags '-extldflags "-static"' ./cmd/kubelet/kubelet.go ; ls -l kubelet ; strip kubelet ; ls -l kubelet
-rwxr-xr-x 1 compsciphd compsciphd 105767304 Jan 3 15:10 kubelet
-rwxr-xr-x 1 compsciphd compsciphd 58116640 Jan 3 15:10 kubelet
</code></pre>
Why are statically linked cgo binaries smaller than regularly compiled dynamically linked binaries?
blov · · 620 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传