英文:
gccgo -static vs -static-libgo
问题
-static
和 -static-libgo
在 gccgo
中有什么区别?文档似乎没有真正解释发生了什么:
- 使用
-static-libgo
选项对编译的包进行静态链接。 - 使用
-static
选项进行完全静态链接(gc 编译器的默认选项)。
-static-libgo
只是对 libgo.a
进行静态链接吗?而 -static
是完整的 glibc 库静态链接?
英文:
What is the difference between -static
and -static-libgo
for gccgo
? The documentation doesn't seem to really shed light on what is going on:
- Use the
-static-libgo
option to link statically against the compiled packages. - Use the
-static
option to do a fully static link (the default for the gc compiler).
Is -static-libgo
only static linking libgo.a
only? While -static
is full glibc library?
答案1
得分: 11
检查生成的ELF文件中的动态链接:
使用gc静态构建:
$ go build hello.go
$ readelf -d hello
此文件中没有动态部分。
gccgo默认动态链接到libgo、libc等:
$ go build -compiler gccgo hello.go
$ readelf -d hello
偏移量为0x36e0的动态部分包含29个条目:
标签 类型 名称/值
0x0000000000000001 (NEEDED) 共享库: [libgo.so.5]
0x0000000000000001 (NEEDED) 共享库: [libm.so.6]
0x0000000000000001 (NEEDED) 共享库: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) 共享库: [libc.so.6]
0x0000000000000001 (NEEDED) 共享库: [ld-linux-x86-64.so.2]
0x0000000000000001 (NEEDED) 共享库: [libpthread.so.0]
将libgo嵌入可执行文件中,但仍然动态链接到libc等:
$ go build -compiler gccgo -gccgoflags '-static-libgo' hello.go
$ readelf -d hello
偏移量为0x128068的动态部分包含28个条目:
标签 类型 名称/值
0x0000000000000001 (NEEDED) 共享库: [libpthread.so.0]
0x0000000000000001 (NEEDED) 共享库: [libm.so.6]
0x0000000000000001 (NEEDED) 共享库: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) 共享库: [libc.so.6]
0x0000000000000001 (NEEDED) 共享库: [ld-linux-x86-64.so.2]
静态链接所有内容:
$ go build -compiler gccgo -gccgoflags '-static' hello.go
$ readelf -d hello
此文件中没有动态部分。
英文:
Check the dynamic linkage in the generated ELFs:
gc builds statically:
$ go build hello.go
$ readelf -d hello
There is no dynamic section in this file.
gccgo links dynamically against libgo, libc etc. by default:
$ 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]
Bake libgo inside the executable, but still link dynamically to libc and friends:
$ 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]
Link everything statically:
$ go build -compiler gccgo -gccgoflags '-static' hello.go
$ readelf -d hello
There is no dynamic section in this file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论