Erlang Central

Cross compiling

Revision as of 10:32, 7 September 2006 by Matthias (Talk | contribs)



Matthias Lang, with important ideas from Brian Zhou

Why Cross Compile

Cross compiling means using one type of system to compile the Erlang runtime system for a different type of system. A typical example would be using an x86-based linux system to compile Erlang for an embedded MIPS CPU.

You usually cross compile when you want to run Erlang on a system which doesn't have its own development environment, i.e. no C compiler, libraries and make system.

In this howto, the term target means the system you want to run Erlang on when you're finished. The term build platform means the system you're using to compile the Erlang runtime system.


  • A working C compiler and C libraries. Verify that it generates object files which run on the target, for instance by writing hello world. This howto assumes that your build platform is a unix or unix-like system.
  • The autoconf tools.
  • The Erlang/OTP source code, from The examples assume R10B-10.
  • Some cross-compiling experience. If you've never cross-compiled before, it's probably worth starting with something a bit simpler than Erlang, just to get a feeling for how it's meant to work. One suggestion: busybox.
  • Experience with native compiling Erlang. If you haven't built the Erlang runtime from source before, do that first, to get a feeling for how it's meant to work.
  • A generous serving of gumption.

Unpack the source

Make a directory for the cross compiling work, say /usr/local/src/mips_erlang/ and untar the source distribution there.

Patch the configure script

A couple of small changes to the configure script supplied with the Erlang source makes cross compiling much easier. Take this patch:

---   2006-03-17 17:38:39.000000000 +0100
+++        2006-03-17 17:29:23.000000000 +0100
@@ -1322,7 +1322,7 @@
-], poll_works=true, poll_works=false, poll_works=false)
+], poll_works=true, poll_works=false, poll_works=true)
 case $poll_works in
@@ -1365,7 +1365,12 @@
+if test "x$LD" = x; then
+ DED_LD=ld

and apply it in the erts/ directory in the source tree you just unpacked.

After applying the patch, you need to re-run autoconf to update the configure script:

cd erts
autoconf > configure

Set environment variables for non-detectable features

In a native compile, the 'configure' script autodetects many settings by compiling and running small C programs. When cross compiling, this isn't possible, so you need to do some manual setup to override the autodetection:

export ac_cv_prog_javac_ver_1_2=no 
export ac_cv_c_bigendian=yes
export ac_cv_func_setvbuf_reversed=no
export ac_cv_func_mmap_fixed_mapped=yes
export ac_cv_sizeof_long_long=8 
export ac_cv_sizeof_off_t=8

The values of the variables above are correct for an AU1000 MIPS CPU running linux 2.4.x. They're probably not correct for your particular system. You need to manually figure out the right settings, i.e. is your system bigendian? How large is a long_long on your system? If you're not sure, write a short C program to find out, and run the C program on your target.

Set environment variables to set up the C compiler

Next, we need to set up the environment so that we use the right C compiler and related tools:

    export PATH=$PATH:/usr/local/eldk31/usr/bin/
    export CC=mips-linux-gcc
    export CXX=mips-linux-g++
    export SHLIB_LD=mips-linux-gcc
    export LD=mips-linux-ld
    export AR=mips-linux-ar
    export RANLIB=mips-linux-ranlib
    export CROSS_COMPILE=mips_4KC-
    export CFLAGS=-Os

The above settings are correct for an Au1000 MIPS on linux target. Your settings are likely to be different.

Manually disable parts of HiPE


Run the configure script

./configure --prefix=/tmp/cross_compiled --without-ssl --without-java --disable-hipe --host=mips-linux --build=i686-pc-linux-gnu

Doesn't hurt to double-check some settings in config.h after this runs.

Run make


Manual corrections

Fix the path in bin/erl