1

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?

jackme
  • 11
  • 1
  • 4
  • Show the code of the program you're trying to compile. For a simple hello world program `go build -compiler gccgo -gccgoflags "-static" hello.go` works for me. – Ainar-G Nov 02 '15 at 12:33
  • An error like "cannot find -lpthread" implies that you do not have the static version of the pthread library on your system. You need to have libpthread.a somewhere, such as /usr/lib or /usr/lib/x86_64-linux-gnu. If you don't, you need to install it. That may also be the problem with libgo; it depends on how you installed gccgo. – Ian Lance Taylor Nov 02 '15 at 19:52
  • package main //import "os"// 用于获得命令行参数os.Args import "fmt" //import "strconv" func main() { var a = "asdf" const( c0=iota c1=iota c2=iota ) var v_111 int v_111 = 111 fmt.Println("complex dsf ", v_111) fmt.Printf("Result: %d %d %d %s\n", c0, c1, c2, a) var str string str = "hello world" for i :=0; i< len(str); i++{ fmt.Println(i, str[i]) } //array ar_b :=[5] byte{1,2,3,4,15} for _ , v :=range ar_b{ fmt.Println(v, " ") } } – jackme Nov 03 '15 at 01:20
  • I use -L/lib64 in build command line to set the library search path, because there are all lib files on this dir – jackme Nov 03 '15 at 01:25
  • ps why I set -L/lib64 because: ldd test_gccgo_yes linux-vdso.so.1 => (0x00007fff8e9fe000) libgo.so.4 => /lib64/libgo.so.4 (0x00007fd4bf2f0000) libm.so.6 => /lib64/libm.so.6 (0x00007fd4befee000) libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fd4bedd7000) libc.so.6 => /lib64/libc.so.6 (0x00007fd4bea16000) /lib64/ld-linux-x86-64.so.2 (0x00007fd4c0023000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd4be7fa000) – jackme Nov 03 '15 at 01:27

1 Answers1

2

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

rjh
  • 49,276
  • 4
  • 56
  • 63