duankeng1911 2014-03-08 08:50
浏览 409
已采纳

通过ldflags选项传递给go命令时,w标志是什么意思?

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 ?
  • 写回答

4条回答 默认 最新

  • douliaotong4944 2014-03-08 23:10
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记