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 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含