[英]Go package linkage with a C library
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:
我正在尝试构建一个Go包,其中包含用C编写的库中的函数。结构基本如下:
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
. 跑步
go build foo.go
失败,出现“未定义的引用”为orig_func
。 Note that the header is mybar.h
; 请注意,标题是
mybar.h
; I created a prototype for orig_func
that was not included in the original library. 我创建了原始库中未包含的
orig_func
原型。 Do I need to recompile the library first, including this header file, before it will link with the Go build? 我是否需要首先重新编译库,包括此头文件,然后才能与Go构建链接? 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 the configure
/ make
(or whatever) to compile the library; cgo
不能替换configure
/ make
(或其他)来编译库; it only knows how to build a few .c
files in your package directory, and a library's build process might be more complex. 它只知道如何在包目录中构建一些
.c
文件,并且库的构建过程可能更复杂。
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. 使用
cgo
交叉编译的(关闭)Go bug在这里看起来很有用。 You may want to build the Go toolchain with some environment variables set that are described in godoc cmd/cgo
: 您可能希望使用
godoc cmd/cgo
中描述的一些环境变量集构建Go工具链 :
To enable cgo during cross compiling builds, set the CGO_ENABLED environment variable to 1 when building the Go tools with make.bash.
要在交叉编译构建期间启用cgo,请在使用make.bash构建Go工具时将CGO_ENABLED环境变量设置为1。 Also, set CC_FOR_TARGET to the C cross compiler for the target.
另外,将CC_FOR_TARGET设置为目标的C交叉编译器。 CC will be used for compiling for the host.
CC将用于编译主机。
After the Go tools are built, when running the go command, CC_FOR_TARGET is ignored.
构建Go工具后,运行go命令时,将忽略CC_FOR_TARGET。 The value of CC_FOR_TARGET when running make.bash is the default compiler.
运行make.bash时CC_FOR_TARGET的值是默认编译器。 However, you can set the environment variable CC, not CC_FOR_TARGET, to control the compiler when running the go tool.
但是,您可以在运行go工具时设置环境变量CC而不是CC_FOR_TARGET来控制编译器。
CXX_FOR_TARGET works in a similar way for C++ code.
CXX_FOR_TARGET的工作方式与C ++代码类似。
The bug also mentions someone who uses -ldflags="-extld=$(CC)"
(where $(CC)
is the name of the cross-compiler they want to use). 该bug还提到了使用
-ldflags="-extld=$(CC)"
(其中$(CC)
是他们想要使用的交叉编译器的名称)。
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. 在你的示例代码中有一个显式的
-L/usr/local/lib
,我认为这不会起作用:我认为当你为目标构建库时,你会想要将它们放在一个不同的目录中主机拱的lib
。 For example, this ARM cross-compilation HOWTO uses a /usr/local/arm-linux
prefix or install_root in some places. 例如, 这个ARM交叉编译HOWTO在某些地方使用
/usr/local/arm-linux
前缀或install_root。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.