C语言 如何在 Go 中构建发布版本的二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29599209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:51:17  来源:igfitidea点击:

How to build a release version binary in Go?

cgobuildrelease

提问by catric mia

In C, we can build a debug version or a release version of the binary files (the object files and the executable). How can we do this in Go?

在 C 中,我们可以构建二进制文件(目标文件和可执行文件)的调试版本或发布版本。我们如何在 Go 中做到这一点?

回答by hiwjd0

In Go, it isn't typical to have a debug version or a release version.

在 Go 中,通常没有调试版本或发布版本。

By default, go buildcombines symbol and debug info with binary files. However, you can remove the symbol and debug info with go build -ldflags "-s -w".

默认情况下,go build将符号和调试信息与二进制文件结合起来。但是,您可以使用 删除符号和调试信息go build -ldflags "-s -w"

回答by rob74

You can instruct the linker to strip debug symbols by using

您可以使用以下命令指示链接器剥离调试符号

go install -ldflags '-s'

I just tried it on a fairly large executable (one of the GXUIsamples), and this reduced it from ~16M to ~10M. As always, your mileage may vary...

我只是在一个相当大的可执行文件(GXUI示例之一)上尝试了它,这将它从 ~16M 减少到 ~10M。与往常一样,您的里程可能会有所不同...

Hereis a full list of all linker options.

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