Can I control static or dynamic library linking while compiling? Does each create diff. binary sizes?

huangapple go评论141阅读模式
英文:

Can I control static or dynamic library linking while compiling? Does each create diff. binary sizes?

问题

在一个Go语言研讨会上,讲师说当他使用静态链接编译Go应用时,生成的二进制文件大小约为600MB,但当他使用动态链接编译相同的应用时,生成的二进制文件大小变为了10MB。

我不确定他在说什么,编译时的动态链接和静态链接是否会对二进制文件的大小产生如此大的差异,而且我是否能够控制这个差异?

英文:

In one of the Go seminars, the lecturer said that when he compiled the Go application, statically linked, the size of the resulting binary was about 600 MB big, but when he compiled the same app with dynamic linking, the resulting binary turned into 10 MB.

I'm not sure what he is talking about, can dynamic vs. static linking during compilation make that difference in space of binary, and do I have control over that?

答案1

得分: 5

默认情况下,Go使用静态链接,所以所有的代码和包源代码都会编译成一个大的二进制文件。

自从Go 1.5发布以来,你可以使用-buildmode=shared选项来编译Go共享库,使用go buildgo install命令。然后你可以使用-linkshared标志来编译你的应用程序二进制文件。详细信息可以在这里找到。

当然,如果你动态链接包,你的二进制文件大小会比静态链接小,但是总的应用程序大小不会减小,因为你只是将你的代码放在了其他地方。
因此,只有在你需要在不同的应用程序之间共享相同的包时,动态链接才有意义。

英文:

By default Go uses static linking so everything (your code and packages sources) compiles in one big binary.

Since Go 1.5 released you can compile Go shared library using -buildmode=shared option for go build or go install. Then you can compile your app binary with -linkshared flag. Details can be found here.

Ofcourse, if you link packages dynamically your binary size will be less than with static linking, but total application size will not be reduced because you just "put your code in other place".
So dynamic linking makes sense only if you need to share the same packages between different apps.

huangapple
  • 本文由 发表于 2016年2月24日 16:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/35596415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定