gccgo -static vs -static-libgo

时间:2013-12-04 07:03:06

标签: go static-linking gccgo

-static的{​​{1}}和-static-libgo之间有什么区别?文档似乎并没有真正揭示正在发生的事情:

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

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

1 个答案:

答案 0 :(得分:11)

检查生成的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.