Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to statically link a Go binary that uses crypto/tls?

If you try to compile the following Go program:

package main

import _ "crypto/tls"

func main() {

}

You'll wind up with a dynamically linked Go binary. This is annoying for me (I'm building a Go binary inside a Docker container, which uses a different libc from my host, which will result in the binary not running on my host).

How does one force Go to build such a program statically?

like image 866
cyphar Avatar asked Jan 17 '16 15:01

cyphar


People also ask

Are Go binaries statically linked?

GoLang by default produces statically linked binaries. RustLang by default produces dynamically linked binaries.

Are Go binaries static?

Go creates static binaries by default unless you use cgo to call C code, in which case it will create a dynamically linked binary.

Does Go support dynamic linking?

Go does not currently support dynamic linking. Well, it can link C . dll/. so files okay, but there's no way to dynamically link to a Go binary (or create a .

Is Go statically compiled?

Statically-Linked means that all the code you use from Go's standard library, as well as third-party packages, is compiled into your built executable. When combined with the fully-compiled quality, this means that distributing a Go program to another machine is literally as simple as copying a single file.


1 Answers

The only OS where crypto/tls uses cgo is darwin, where it needs to call FetchPEMRootsto get the root CAs.

The reason your program is using cgo is because crypto/tls imports the net package, which links to the host resolver by default. You can build the net package without cgo using the "netgo" build tag.

go build -tags netgo

Or if you're on a release where the std lib packages won't be built by default, you can trigger them to be compiled with a new installsuffix

go build -installsuffix netgo -tags netgo

Since you're not going to have the need or ability to use cgo in your environment, you may want to just build everything with CGO_ENABLED=0 to disable cgo entirely.

like image 141
JimB Avatar answered Sep 30 '22 15:09

JimB