Cross-compile for MIPS arch (openwrt)

Alex Egg,

This post describes how to build software that will run on openwrt. Or more generally, it’s a post about how to cross-compile for MIPS architecture.

My goal was to build a hello-world C app on my mac (64bit x86 arch.) for my openwrt router (MIPS arch.). This is called cross-compiling: building for one arch. on another.

In theory it’s simple: you just past gcc the -mach argument for the arch. you want:

gcc -march=i386 -o MyProgram sourcefile.c

However, this depends on what arch’s you included when you built your gcc. I want to build for my router which is a MIPS arch. This didn’t come w/ my gcc.

An easy way to get a compatible gcc for openwrt is to use the openwrt buildroot: This will get you all the tools you need. It builds gcc, binutils, etc – your whole tool chain. If you look in you openwrt build dir in: staging/bin/ you’ll see a gcc that’s build just for your arch. In my case MIP.

Then just build it like normal:

$ mipsel-openwrt-linux-uclibc-gcc hello.c -o hello_mips

Now check the arch:

$ file hello_mips 
hello_mips: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1, 
dynamically linked (uses shared libs), with unknown 
capability 0x41000000 = 0xf676e75, with unknown 
capability 0x10000 = 0x70403, not stripped

Now I can copy this binary to my router and run it:

$ ./hello_mips
Hello, World!

Permalink: cross-compile-mips-openwrt


Last edited by Alex Egg, 2016-10-05 19:16:47
View Revision History