Precompiling part of Go program that relies on C for speed

338 views Asked by At

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

There are 1 answers

0
1lann On

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.