dongsao8279 2013-02-04 10:30
浏览 130
已采纳

用gc和gccgo编译的静态链接二进制文件的奇怪行为

here is a Hello world in go:

package main                                                                                                                                               

import (                                                                      
  "fmt"                                                                    
  )                                                                         

func main() {                                                                 
        fmt.Println("Go is great!")                                           
}

Put it in hello.go and compile it with:

  • go build -o hello_go_build hello.go
  • go build -o hello_go_build_gccgo --compiler gccgo hello.go
  • gccgo -o hello_gccgo_shared hello.go
  • gccgo -static -o hello_gccgo_static hello.go

First, I noticed hello_go_build_gccgo and hello_gccgo_shared are not of the same size. I looked for information on the Internet without success. Does anyone know why is that? Or even better, would anyone tell me how I can try to figure that out? I tried to keep temp files with the -work flag, but I couldn't spot the relevant information.

Then, as you might notice, the two statically linked binaries does not have the same size either. Actually, the one compiled with the go build (hello_go_build) command works not only on my system but also on other systems with other Linux distribution while hello_go_build_gccgo fails on my system as well as on others with the error:

panic: runtime error: invalid memory address or nil pointer dereference

This is a bug about to be solved: https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/y2RIy0XLJ24

Finally, even if nowadays, size does not matter anymore, I am curious: is there any option with anyone of the go compilers to do function level linking (instead of statically link a package as a whole, only link the functions needed and their dependencies)?

  • 写回答

1条回答 默认 最新

  • douxuanou2787 2013-02-04 11:08
    关注

    First, I noticed hello_go_build_gccgo and hello_gccgo_shared are not of the same size. I looked for information on the Internet without success. Does anyone know why is that?

    I would find it odd if they would be the same size. One is statically linked, the other uses shared libraries, so why they should be expected to be the same size?

    Then, as you might notice, the two statically linked binaries does not have the same size either.

    I would find it odd if they would be the same size. One is compiled by gc, the other by gccgo - two completely different compilers. Why they should be expected to produce a binary of the same size?

    Finally, even if nowadays, size does not matter anymore, I am curious: is there any option with anyone of the go compilers to do function level linking (instead of statically link a package as a whole, only link the functions needed and their dependencies)?

    There's no such thing as "statically link a package as a whole" with gc. Unused functions (and perhaps not only functions) are not present in the binary. And, IIRC, that was the case since day 1 (counting from the public release). Not sure if the preceding applies to gccgo as well, but I would expect it to do the same good job in this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号如何开发网页
  • ¥15 h3.6m 人类行为预测论文复现
  • ¥50 wordpress项目注册报失败刷新后其实是成功状态,请求排查原因
  • ¥20 linxu服务器僵尸进程不释放,代码如何修改?
  • ¥15 pycharm激活不成功
  • ¥40 如果update 一个列名为参数的value
  • ¥15 基于51单片机的水位检测系统设计中LCD1602一直不显示
  • ¥15 OCS2安装出现问题,请大家给点意见
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上