【问题标题】:gccgo -static vs -static-libgogccgo -static 与 -static-libgo
【发布时间】:2013-12-04 07:03:05
【问题描述】:

对于gccgo-static-static-libgo 有什么区别?文档似乎并没有真正阐明正在发生的事情:

  • 使用-static-libgo 选项静态链接已编译的包。
  • 使用-static 选项进行完全静态链接(gc 编译器的默认设置)。

-static-libgo 是否仅静态链接 libgo.a-static 是完整的 glibc 库吗?

【问题讨论】:

  • 我认为 -static 会尝试静态链接所有内容,而 -static-libgo 要么仅对 Go 包执行此操作,要么仅静态链接 libgo.a。

标签: go static-linking gccgo


【解决方案1】:

检查生成的ELF中的动态链接:

gc 静态构建:

$ go build hello.go
$ readelf -d hello
There is no dynamic section in this file.

gccgo 默认动态链接 libgo、libc 等:

$ go build -compiler gccgo hello.go
$ readelf -d hello
Dynamic section at offset 0x36e0 contains 29 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libgo.so.5]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]

在可执行文件中烘焙 libgo,但仍然动态链接到 libc 和朋友:

$ go build -compiler gccgo -gccgoflags '-static-libgo' hello.go
$ readelf -d hello
Dynamic section at offset 0x128068 contains 28 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [ld-linux-x86-64.so.2] 

静态链接所有内容:

$ go build -compiler gccgo -gccgoflags '-static' hello.go
$ readelf -d hello
There is no dynamic section in this file.

【讨论】:

    猜你喜欢
    • 2012-10-29
    • 2014-11-29
    • 2015-10-04
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2020-06-17
    相关资源
    最近更新 更多