I hope this is a basic question. I am trying to build a Go package which includes functions from a library written in C. The structure is basically as follows:
package too
/*
#cgo LDFLAGS: -L/usr/local/lib include -lbar
#include mybar.h
*/
import "C"
func MyGoWrapper () {
C.orig_func()
}
Running go build foo.go
fails with an "undefined reference" for orig_func
. Note that the header is mybar.h
; I created a prototype for orig_func
that was not included in the original library. Do I need to recompile the library first, including this header file, before it will link with the Go build? Or am I misunderstanding something else entirely?
When linking against an external library, you do need to separately compile it for your target architecture.
cgo
can't replace theconfigure
/make
(or whatever) to compile the library; it only knows how to build a few.c
files in your package directory, and a library's build process might be more complex.I'm less sure of how to accomplish the larger task of linking in an external library when cross-compiling (and I'm not sure what you've already done). The (closed) Go bug on cross-compilation with
cgo
looks useful here. You may want to build the Go toolchain with some environment variables set that are described ingodoc cmd/cgo
:The bug also mentions someone who uses
-ldflags="-extld=$(CC)"
(where$(CC)
is the name of the cross-compiler they want to use).In your example code there's an explicit
-L/usr/local/lib
and I don't think that'll work: I think when you build libraries for the target, you're going to want to put them in a directory distinct from thelib
for your host arch. For example, this ARM cross-compilation HOWTO uses a/usr/local/arm-linux
prefix or install_root in some places.