如何用gccgo构建静态程序

时间:2015-11-02 11:23:35

标签: go static gccgo

当我使用gccgo构建静态程序版本

时,我有一个问题

1>使用go build go build -compiler gccgo -gccgoflags'-static -L / lib64'test.go 结果:

import json
# ...
list = json.loads(request.GET['arr'])

2>使用gccgo构建 gccgo -o test_gccgo_yes -static -L / lib64 test.go 结果:

/usr/bin/ld: cannot find -lgo
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc

3>如果我不使用静态编译它 gccgo -o test_gccgo_yes -g test.go 结果: ldd test_gccgo_yes show test_gccgo_yes是动态文件

如何使用gccgo构建静态程序?

1 个答案:

答案 0 :(得分:1)

如果您使用的是static,则gccgo需要每个库的静态版本,即libc.a,而不是动态库libc.so

安装发行版的静态包。在CentOS 7上,它们被命名为glibc-staticlibgo-static。然后你应该能够构建(你也不需要-L标志)

但是,在此之后您可能仍会收到一些警告和可能的错误。例如,在构建一个这样的静态应用程序时,我遇到了这些错误:

/usr/lib/gcc/x86_64-redhat-linux/4.8.5/libgo.a(net.o): In function `net.lookupPort':
(.text+0x48e2): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie

因此可能需要更多工作来获得有效的静态二进制文件。见https://www.akkadia.org/drepper/no_static_linking.html