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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?