When linking against an external library, you do need to separately compile it for your target architecture. cgo
can't replace the configure
/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 in godoc cmd/cgo
:
To enable cgo during cross compiling builds, set the CGO_ENABLED
environment variable to 1 when building the Go tools with make.bash.
Also, set CC_FOR_TARGET to the C cross compiler for the target. CC will
be used for compiling for the host.
After the Go tools are built, when running the go command, CC_FOR_TARGET
is ignored. The value of CC_FOR_TARGET when running make.bash is the
default compiler. However, you can set the environment variable CC, not
CC_FOR_TARGET, to control the compiler when running the go tool.
CXX_FOR_TARGET works in a similar way for C++ code.
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 the lib
for your host arch. For example, this ARM cross-compilation HOWTO uses a /usr/local/arm-linux
prefix or install_root in some places.