英文:
how to build static program with gccgo
问题
我在使用gccgo构建静态程序版本时遇到了问题。
1> 使用go build命令:
go build -compiler gccgo -gccgoflags '-static -L/lib64' test.go
结果:
/usr/bin/ld: 找不到 -lgo
/usr/bin/ld: 找不到 -lpthread
/usr/bin/ld: 找不到 -lm
/usr/bin/ld: 找不到 -lc
2> 使用gccgo build命令:
gccgo -o test_gccgo_yes -static -L/lib64 test.go
结果:
/usr/bin/ld: 找不到 -lgo
/usr/bin/ld: 找不到 -lpthread
/usr/bin/ld: 找不到 -lm
/usr/bin/ld: 找不到 -lc
3> 如果我不使用静态编译:
gccgo -o test_gccgo_yes -g test.go
结果:
ldd test_gccgo_yes
显示test_gccgo_yes是一个动态文件
请问如何使用gccgo构建静态程序?
英文:
I has a prblem when I use gccgo to build static programe version
1> use go build
go build -compiler gccgo -gccgoflags '-static -L/lib64' test.go
result:
/usr/bin/ld: cannot find -lgo
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
2>use gccgo build
gccgo -o test_gccgo_yes -static -L/lib64 test.go
result:
/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> if I don't use static to compile it
gccgo -o test_gccgo_yes -g test.go
result :
ldd test_gccgo_yes
show test_gccgo_yes is dynamic file
How I can build static program with gccgo?
答案1
得分: 2
如果你使用static
关键字,那么gccgo需要每个库的静态版本,例如libc.a
而不是动态库libc.so
。
安装你的发行版的静态包。在CentOS 7上,它们的名称是glibc-static
和libgo-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。
英文:
If you're using static
then gccgo requires the static versions of each library i.e. libc.a
rather than the dynamic libs libc.so
.
Install your distro's static packages. On CentOS 7, they are named glibc-static
and libgo-static
. Then you should be able to build (you won't need the -L
flag either)
However, you may still get some warnings and possibly errors after that. For example when building one such static application I got these errors:
/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
So more work may be required to get a working static binary. See https://www.akkadia.org/drepper/no_static_linking.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论