英文:
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。
-w
标志在哪里有记录?它实际上是做什么的?- 然后我发现了
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.
- Is this
-w
flag documented anywhere? What does it actually do? - 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 -s
、strip -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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论