Skip to content

Instantly share code, notes, and snippets.

@janetkuo
Last active February 16, 2023 08:56
Show Gist options
  • Select an option

  • Save janetkuo/a478d7fac3e3500173a83be2496772b2 to your computer and use it in GitHub Desktop.

Select an option

Save janetkuo/a478d7fac3e3500173a83be2496772b2 to your computer and use it in GitHub Desktop.
How to build statically linked release binary, take https://github.com/skippbox/kompose as an example

How to build statically linked release binary

Check if a binary is statically linked

# List dynamic dependencies (shared libraries):
# 1. if it's dynamically linked, you'll see
$ ldd kompose 
    linux-vdso.so.1 =>  (0x00007ffe937ea000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f0a7dae5000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0a7d720000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f0a7dd03000)
# 2. if it's statically linked, you'll see
$ ldd kompose 
	not a dynamic executable

# Recognize the type of data in a file
# 1. if it's dynamically linked, you'll see
$ file kompose 
/usr/local/kompose: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=86c6d2ff21297a06cc7319244f35e2671612beae, not stripped
# 2. if it's statically linked, you'll see
$ file kompose 
/usr/local/kompose: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), statically linked, not stripped

Build statically linked go binaries

Go executables are statically linked, except when they are not. If you want them to be statically linked, go needs to be compiled without cgo support. To do that,

# Disable cgo
export CGO_ENABLED=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment