Golang可执行文件大小

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

Golang executable size

问题

我已经搜索了关于构建紧凑的Go可执行文件的最新信息,但没有成功。大部分信息似乎都是几年前的。

我的程序是在Rosettacode.org上找到的标准的Hello World程序。使用Fortran编译后,得到的exe文件大小不到100 KB。而使用Go编译后,得到的exe文件大小高达2400 KB。

我的问题是:是否有编译器开关或编译指令我应该使用?是否有其他工具或技巧我不知道(我是Go的初学者)?关于可执行文件大小,Go的未来计划是什么?在Linux下情况如何?在大型程序中,exe文件会发生什么变化?

背景信息:我正在使用安装在Windows 7上的Go 1.5。我的项目是编写(实际上是从其他语言迁移)一些命令行程序。拥有20个或更多的大型Go可执行文件会占用很多空间。好吧,我知道空间很便宜,所以我可以接受。然而,我相信对这个问题的澄清将会帮助很多人。

英文:

I've searched for recent information on building compact Go executables, without success. Most information appears to be several years old.

My program: the standard hello world as found on Rosettacode.org.
With Fortran I get an exe file under 100 KB. With Go the exe file is whopping 2400 KB.

My questions: are there compiler switches or pragmas that I should be using? Are there other tools or tricks I am not aware of (I am a beginner with Go). What are the future plans for Go regarding executable size? What is it like under Linux - same thing? What happens to exe files on large programs?

Background: I'm using go 1.5 installed on Windows 7. My project is writing (actually migrating from other languages) some command line programs. Having 20 or more large Go exe files eats up a lot of space. OK, I know space is cheap so I can live with it. However, I believe clarity on the matter will help many people.

答案1

得分: 4

一个Go程序之所以庞大,是因为它包含了垃圾回收代码、goroutine调度代码,除此之外几乎所有的库都是静态链接的。这是设计上的选择。

常见问题:为什么我的简单程序会生成如此庞大的二进制文件?

英文:

A Go program is big because it contains garbage collaction code, goroutine scheduling code and besides that it has nearly all libraries statically linked. It is so by design.

FAQ Why is my trivial program such a large binary

答案2

得分: 3

为了减小可执行文件的大小,你可以通过以下方式指示链接器去除调试符号:

go install -ldflags '-s'
go install -ldflags '-s -w'

我在一个相当大的可执行文件(GXUI示例之一)上尝试了这个方法,将文件大小从约16M减小到约10M。添加-w到-s没有任何区别,但是请注意,效果因人而异...

这里是所有链接器选项的完整列表。

英文:

To reduce the executable size, you can instruct the linker to strip debug symbols by using one of the following:

go install -ldflags '-s'
go install -ldflags '-s -w'

I tried it on a fairly large executable (one of the GXUI samples), and this reduced it from ~16M to ~10M. Adding -w to -s didn't make any difference, but as always, your mileage may vary...

Here is a full list of all linker options.

huangapple
  • 本文由 发表于 2015年9月17日 17:38:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/32627080.html
匿名

发表评论

匿名网友

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

确定