Statically (cross) compiled vim for x86, x86-64 and mipsel

17-09-2014 | Remy van Elst


Table of Contents


Sometimes I need to manage a few systems with either low resources or a very restricted set of packages. On those systems no compilers or development libraries are available, however it is allowed to bring binaries.

A few of those systems are 32 bit x68 systems, some are MIPS systems, even worse. They serve a secure purpose, I cannot go in to much detail about them, except for they require a high level of security, they process certificates.

I really like vim as my editor, the only editor available by default on those systems is ed. I have an ed cheatsheet for this purpose.

The solution for this problem is to create a statically (cross) compiled version of vim.

These instructions are for Debian/Ubuntu, tested on an Ubuntu 14.04 64 bit machine.

If you like this article and want to support me AND get $10 Digital Ocean credit (2 months free), use this link to order: https://www.digitalocean.com/?refcode=7435ae6b8212

Install required packages

First install the required development libraries and git:

apt-get install libncurses5-dev git build-essential

If you need to cross compile from 64 bit to 32 bit, also install libc6-dev-i386:

apt-get install libc6-dev-i386

The MIPS hardware has a special toolchain which have specific instructions not covered here. The manufacturer probably covers this in their documentation.

Vim source code

Clone the vim source from github:

git clone https://github.com/b4winckler/vim.git

Go to the vim/src folder:

cd vim/src/

Compile time

Set the compile flags for vim and start the compile:

export LDFLAGS="-static"
export MAKEFLAGS="-j4"
export CFLAGS=""
./configure --with-features=small  --with-compiledby='Remy <relst@relst.nl>'  --with-x=no  --disable-gui  --disable-netbeans  --disable-pythoninterp  --disable-python3interp  --disable-rubyinterp  --disable-luainterp 

The above LDFLAGS make sure Vim gets statically compiled. The other options disable any GUI support (x) and enable a portion of the features, namely the small featureset. This lacks syntax highlighting and such. See below for a vim :version output.

If you need to compile on a 64 bit machine for a 32 bit machine, export the following CFLAGS:

export CFLAGS="-m32"

To compile for mips:

./configure --with-features=small  --with-compiledby='Remy <relst@relst.nl>'  --with-x=no  --disable-gui  --disable-netbeans  --disable-pythoninterp  --disable-python3interp  --disable-rubyinterp  --disable-luainterp --target=mipsel-safenet # or mipsel-linux

Start the actual compile:

make

Results

When it is finished you should have a fairly small vim file:

$ ls -la vim
-rwxr-xr-x 1 remy remy 1.4M Sep 17 04:06 vim

Check that it is statically linked:

$ ldd vim
    not a dynamic executable

A non-statically compiled vim gives me this:

$ ldd /usr/bin/vim
        linux-gate.so.1 =>  (0xb77c7000)
        libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb7799000)
        libtinfo.so.5 => /lib/i386-linux-gnu/libtinfo.so.5 (0xb7779000)
        libselinux.so.1 => /lib/i386-linux-gnu/libselinux.so.1 (0xb7758000)
        libacl.so.1 => /lib/i386-linux-gnu/libacl.so.1 (0xb774e000)
        libgpm.so.2 => /usr/lib/i386-linux-gnu/libgpm.so.2 (0xb7748000)
        libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75fa000)
        libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb75f6000)
        /lib/ld-linux.so.2 (0xb77c8000)
        libattr.so.1 => /lib/i386-linux-gnu/libattr.so.1 (0xb75ef000)

You can also use the file command:

$ file vim
vim: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.26, BuildID[sha1]=0x97cb04dde25cd539487369524b0787fd422044be, not stripped

You can decease the filesize with 0.1 MB by stripping:

$ strip vim
$ ls -la vim
-rwxr-xr-x 1 remy remy 1.3M Sep 17 04:04 vim

The same vim, dynamically compiled (without the LDFLAGS="-static"), is a little bit smaller:

$ ls -la vim
-rwxr-xr-x 1 remy remy 793K Sep 17 04:17 vim

That's it. You can copy/scp the vim file anywhere and use it, as long as it is the correct architecture. Even on restricted systems in my case.

These instructions work for almost all programs you can compile. The LDFLAGS="-static" is the magic part here. You can try it for yourself with other software.

Version info

Here is the output of the :version in vim itself:

:version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Sep 17 2014 04:04:32)
Included patches: 1-430
Compiled by Remy <relst@relst.nl>
Small version without GUI.  Features included (+) or not (-):
+acl             -conceal         -farsi           -libcall         -mouse_sgr       -python3         -tag_old_static  -vreplace
-arabic          -cryptv          -file_in_path    -linebreak       -mouse_sysmouse  -quickfix        -tag_any_white   +wildignore
-autocmd         -cscope          -find_in_path    -lispindent      -mouse_urxvt     -reltime         -tcl             -wildmenu
-balloon_eval    -cursorbind      -float           -listcmds        -mouse_xterm     -rightleft       +terminfo        +windows
-browse          -cursorshape     -folding         -localmap        -multi_byte      -ruby            -termresponse    +writebackup
+builtin_terms   -dialog          -footer          -lua             -multi_lang      -scrollbind      -textobjects     -X11
-byte_offset     -diff            +fork()          -menu            -mzscheme        -signs           -title           -xfontset
-cindent         -digraphs        -gettext         -mksession       -netbeans_intg   -smartindent     -toolbar         -xim
-clientserver    -dnd             -hangul_input    -modify_fname    -path_extra      -sniff           -user_commands   -xsmp
-clipboard       -ebcdic          -iconv           -mouse           -perl            -startuptime     -vertsplit       -xterm_clipboard
-cmdline_compl   -emacs_tags      -insert_expand   -mouse_dec       -persistent_undo -statusline      -virtualedit     -xterm_save
+cmdline_hist    -eval            +jumplist        -mouse_gpm       -printer         -sun_workshop    +visual          -xpm
-cmdline_info    -ex_extra        -keymap          -mouse_jsbterm   -profile         -syntax          -visualextra
-comments        -extra_search    -langmap         -mouse_netterm   -python          -tag_binary      -viminfo
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -O2 -fno-strength-reduce -Wall -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc   -static -L/usr/local/lib -Wl,--as-needed -o vim        -lm -ltinfo  -ldl

Tags: compile, gcc, mips, ubuntu, vim,