预编译依赖于 C 以提高速度的 Go 程序的一部分。

huangapple go评论88阅读模式
英文:

Precompiling part of Go program that relies on C for speed

问题

我的Go程序的一部分依赖于一个非常庞大的C代码库,使用import "C",编译这个库需要几分钟的时间。有没有办法预编译这个C库,或者创建一个与C代码一起预编译的Go程序分支,这样每次编译主程序时就不必等待整个C库重新编译?

英文:

Part of my Go program relies on a very large C codebase using import "C" that takes a few minutes to compile. Is there any way to precompile per-se that C library or create a branch of my Go program that will be precompiled along with the C code so that each time I compile the main program I don't have to wait for the entire C library to re-compile each time?

答案1

得分: 3

不要回答我要翻译的问题。以下是翻译好的内容:

与其导入整个C源代码,你可以将其与编译好的目标文件和头文件进行链接。请参考https://golang.org/cmd/cgo/,其中介绍了如何使用cgo的LDFLAGS参数。

还有其他在线文档介绍如何将C代码编译为目标文件(.a.o文件),比如这个。你还应该参考你使用的库的文档或其Makefile,因为它很可能已经包含了将其编译为可链接的目标文件的指令。

如果库中有import "C",并且其源代码没有被修改,你还可以使用go get(或者可能是go install)来获取它,这将把编译好的目标文件存储在你的$GOPATH/pkg中,从而加快导入该库的其他Go程序的编译速度。

英文:

Instead of importing the entire C source code, you can link it with compiled object files and header files. Refer to https://golang.org/cmd/cgo/ which covers how to use the LDFLAGS argument for cgo.

There are other documents online which cover how to compile C code into object files (.a and .o files) such as this one. You should also refer to documentation in the library you're using, or its Makefile as it will likely already have instructions to compile it into object files that can be linked.

If the library that has import "C", and its source isn't being modified, you can also go get it, (or perhaps go install it) which will store its compiled object files in your $GOPATH/pkg, making compilation of other Go programs that import it faster.

huangapple
  • 本文由 发表于 2017年1月2日 14:19:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/41422187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定