Compiling a Go Program with C Libraries into a Standalone Executable

I am trying to compile a go program to be completely self contained, including the c libraries. The command I am using is

[mm17@mm17 grogger]$ go build --ldflags '-extldflags "-static"' ./grogger.go
# command-line-arguments
/usr/bin/ld: cannot find -lgrok
collect2: ld returned 1 exit status
/home/mm17/go/pkg/tool/linux_amd64/6l: running gcc failed: unsuccessful exit status 0x100

      

So I will change the file that calls the grok library to have the following header

  1 package grok
  2
  3 /*
  4 #cgo LDFLAGS: -L /usr/lib/libgrok.so -lgrok
  5 #include <grok.h>
  6 */
  7 import "C"

      

And then running the same command gives

[mm17@mm17 grogger]$ go build --ldflags '-extldflags "-static"' ./grogger.go
# github.com/blakesmith/go-grok
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400180
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_strlen':
../../blakesmith/go-grok/grok.go:169: undefined reference to `strlen'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_patterns_import_from_file':
../../blakesmith/go-grok/grok.go:159: undefined reference to `grok_patterns_import_from_file'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_pattern_add':
../../blakesmith/go-grok/grok.go:147: undefined reference to `grok_pattern_add'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_new':
../../blakesmith/go-grok/grok.go:132: undefined reference to `grok_new'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_match_walk_next':
../../blakesmith/go-grok/grok.go:123: undefined reference to `grok_match_walk_next'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_exec':
../../blakesmith/go-grok/grok.go:81: undefined reference to `grok_exec'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_discover_new':
../../blakesmith/go-grok/grok.go:68: undefined reference to `grok_discover_new'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_compile':
../../blakesmith/go-grok/grok.go:46: undefined reference to `grok_compile'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_match_walk_init':
../../blakesmith/go-grok/grok.go:108: undefined reference to `grok_match_walk_init'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_match_walk_end':
../../blakesmith/go-grok/grok.go:99: undefined reference to `grok_match_walk_end'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_free':
../../blakesmith/go-grok/grok.go:90: undefined reference to `grok_free'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_discover':
../../blakesmith/go-grok/grok.go:58: undefined reference to `grok_discover'
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_free':
../../blakesmith/go-grok/grok.go:34: undefined reference to `free'
collect2: ld returned 1 exit status

      

If I then just try to build it, I also get the same:

[mm17@mm17 grogger]$ go build grogger.go
# github.com/blakesmith/go-grok
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400180
/tmp/go-build294631148/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_strlen':
../../blakesmith/go-grok/grok.go:169: undefined reference to `strlen'
....

      

I'm sure I'm just doing something stupid, but I can't figure it out, I'm pretty newbie when it comes to gcc and goes away, this is my first non-toy program.

+3


source to share


1 answer


When compiling your own library with GCC, be sure to use the -fPIC option.



If the library is compiled without -fPIC GO, it cannot find functions in it.

0


source







All Articles