Statically Link C Programs with musl-gcc

musl-gcc is a wrapper around GCC that uses the musl C standard library implementation to build programs. It is well suited for being linked with other libraries into a single static executable with no shared dependencies.

Getting musl-gcc

The musl-tools package for Debian and Ubuntu provides musl-gcc:

$ sudo apt-get install musl-tools

You can build it from source if a package is not available for your system:

$ tar -xf musl-*.tar.gz && cd musl-*
$ ./configure
$ make && sudo make install

Using musl-gcc

Use musl-gcc in place of gcc. For an autoconf based build, set the $CC variable before the configure script: CC=musl-gcc ./configure Otherwise, append to the end of make: make CC=musl-gcc

Here is an example of building the reference interpreter for the Lua programming language:

$ tar -xf lua-*.tar.gz && cd lua-*
$ make posix CC="gcc -static"
$ ldd src/lua
  not a dynamic executable
$ size src/lua
   text	   data	    bss	    dec	    hex	filename
1702439	   7868	   9768	1720075	 1a3f0b	src/lua

$ make posix CC="musl-gcc -static"
$ ldd src/lua
    not a dynamic executable
$ size src/lua
   text	   data	    bss	    dec	    hex	filename
 259675	   1224	   4960	 265859	  40e83	src/lua

The binary built with musl is nearly 6.5 times smaller.