Getting Statically Linked Binaries under Debian

Jurjen Bokma

March 2009


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

  1. dpkg -S $(which mkdosfs)
    dosfstools: /sbin/mkdosfs

  2. sudo apt-get build-dep dosfstools
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

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

    mkdir src
    apt-get source --build dosfstools 1
    <snip>

    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 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. 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. export CFLAGS=-static
    export LDFLAGS=-static
    export CPPFLAGS=-static
    export CFLAGS_APPEND=-static
    export LDFLAGS_APPEND=-static
    export CPPFLAGS_APPEND=-static


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