如何使用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?

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