Getting Statically Linked Binaries under Debian

March 2009


Say you need e.g. a statically linked mkdosfs under Debian (see ).

  1. figure out which package contains your binary

  2. Fetch the build dependencies for the package

  3. Fetch the source package

    (Make sure you have some deb-src lines in /etc/apt/sources.list)

    1

    You may have to use sudo here. For some programs, some probes by the configure script fail without root permissions, and all you see is that they fail.

  4. find the directory that contains the binary

    find dosfstools-2.11/ -iname mkdosfs -exec file {} \;
    dosfstools-2.11/mkdosfs/mkdosfs: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.8, dynamically linked (uses shared libs), not stripped
    cd dosfstools-2.11/mkdosfs/

  5. Recompile the binary, statically linking

    export CFLAGS=-static
    export LDFLAGS=-static
    export CPPFLAGS=-static
    make mkdosfs
    cc -static mkdosfs.o -o mkdosfs
    file mkdosfs
    mkdosfs: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.8, statically linked, not stripped

As an alternative, you may be lucky seeing that setting the compiler/linker flags before running the apt-get source delivers statically linked binaries immediately:

  1. Set the compiler/linker flags

    export CFLAGS=-static
    export LDFLAGS=-static
    export CPPFLAGS=-static
    export CFLAGS_APPEND=-static
    export LDFLAGS_APPEND=-static
    export CPPFLAGS_APPEND=-static

  2. Create the package


    apt-get source --build dosfstools
    <snip>
    find -iname mkdosfs -type f -exec file {} \;