What does the w flag mean when passed in via the ldflags option to the go command?

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

What does the w flag mean when passed in via the ldflags option to the go command?

问题

上下文:

> go 1.2,ubuntu 12.10

目标:

> 减小编译后二进制文件的大小

目前在我的构建过程中,我运行"go install"来生成二进制文件。
然后我从某个地方读到,如果我传入-w,它会缩小二进制文件。
我尝试了将其传递给-ldflags选项,我的二进制文件大小减小了1MB。

  1. -w标志在哪里有记录?它实际上是做什么的?
  2. 然后我发现了strip -s <binary>命令,并在-w之上运行它,又减小了750KB!生成的二进制文件运行正常。剥离会在任何情况下引起问题吗?
英文:

Context:

> go 1.2, ubuntu 12.10

Goal:

> Reduce size of compiled binaries

Currently in my build process, I run "go install" to generate the binary.
The I read from somewhere that if I pass in -w it will shrink the binary.
I tried it by passing it into the -ldflags option & my binary lost 1MB in size.

  1. Is this -w flag documented anywhere? What does it actually do?
  2. I then discovered the strip -s <binary> command and ran that on top of -w and got
    another weight loss of 750KB ! The resulting binary runs fine. Does stripping
    cause problems in any situations ?

答案1

得分: 78

如果使用-ldflags '-w -s'进行编译,你将得到最小的二进制文件。

-w选项关闭了DWARF调试信息,这意味着你将无法使用gdb来查看特定函数、设置断点或获取堆栈跟踪,因为所有gdb所需的元数据都不会被包含在内。你也将无法使用其他依赖于这些信息的工具,比如pprof性能分析工具。

-s选项关闭了Go符号表的生成,这意味着你将无法使用go tool nm列出二进制文件中的符号。strip -s相当于在-ldflags中传递了-s选项,但它并没有剥离那么多信息。在执行strip -s之后,go tool nm可能仍然可以工作,但我不能完全确定。

这些选项——-ldflags -w-ldflags -sstrip -s——都不会影响实际程序的执行。它们只会影响你是否能够使用其他工具进行调试或分析程序。

英文:

You will get the smallest binaries if you compile with -ldflags '-w -s'.

The -w turns off DWARF debugging information: you will not be able to use gdb on the binary to look at specific functions or set breakpoints or get stack traces, because all the metadata gdb needs will not be included. You will also not be able to use other tools that depend on the information, like pprof profiling.

The -s turns off generation of the Go symbol table: you will not be able to use go tool nm to list the symbols in the binary. strip -s is like passing -s to -ldflags but it doesn't strip quite as much. go tool nm might still work after strip -s. I am not completely sure.

None of these — not -ldflags -w, not -ldflags -s, not strip -s — should affect the execution of the actual program. They only affect whether you can debug or analyze the program with other tools.

答案2

得分: 22

你可以从go tool link获取帮助。

$ go tool link
  ...
  -s    禁用符号表
  -w    禁用DWARF生成
英文:

You can get help from go tool link

$ go tool link
  ...
  -s    disable symbol table
  -w    disable DWARF generation

答案3

得分: 16

go help build中提到:

-ldflags 'flag list'
    在每个5l、6l或8l链接器调用中传递的参数。

因此,我们可以调用go tool 6l来查看它的所有选项之一是

-w	禁用DWARF生成

顺便说一下,5l代表ARM($GOARCH = arm),6l代表x86-64($GOARCH = amd64),8l代表x86($GOARCH = 386)。

如果你真的想查看原始的DWARF信息,你应该在OS X上使用dwarfdump -a,在Linux上使用objdump -wg。警告!输出会很长,非常长。

英文:

The go help build says that

-ldflags 'flag list'
    arguments to pass on each 5l, 6l, or 8l linker invocation. 

So, we can invoke go tool 6l to see all it's options. One of them is

-w	disable DWARF generation 

By the way, 5l stands for ARM ($GOARCH = arm), 6l stands for x86-64 ($GOARCH = amd64), and 8l is for x86 ($GOARCH = 386).

If you really want to view raw DWARF info you should use dwarfdump -a on OS X and objdump -wg on Linux. Warning! Output will be long, very long.

答案4

得分: 0

我担心这可能会在使用Go 1.2的gc工具套件编译的程序中引起问题-请参考这个讨论

总的来说,尽管Go和C一样编译成机器码,但它比C更高级。例如,它内置了详细的panic()堆栈跟踪,这取决于调试信息。gc生成的二进制文件的大小确实可以更小,并且可能在Go 1.3中解决,但实际上,在大多数现代环境中,编译程序的大小并不是一个太大的问题。

英文:

I'm afraid it might cause problems in programs compiled with Go 1.2's gc suite of tools—refer to this discussion.

The general idea is that while Go compiles down to machine code just like C, it's more higher-level than C. For instance, it has built-in detailed panic() stack traces which depend on debug info. The sizes of the gc-generated binaries could indeed have been smaller, and it might be addresseed while cooking Go 1.3, but really the size of a compiled program in most today's environments is not a big deal to be too concerned about.

huangapple
  • 本文由 发表于 2014年3月8日 16:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/22267189.html
匿名

发表评论

匿名网友

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

确定