Cross-compile for OpenWRT

這是一個實驗,在 Ubuntu 上編譯可以在 OpenWRT 上運行的程式。 參考並修改別人的方式,完成了幾個簡單的範例。

Download OpenWRT source code

cd ~
mkdir ~/crosscompile
git clone git://git.openwrt.org/14.07/openwrt.git

Build OpenWRT

cd openwrt
make menuconfig
  1. Choice “Target System” and “Target Profile”
  2. “Advanced configuration options (for developers)” -> “Target Options”
  3. “Advanced configuration options (for developers)” -> “Toolchain Options” -> “Binutils Version” -> “Linaro binutils 2.24”

Save configuration, then:

make defconfg
make

如果嫌 make 不夠快,可以再加上 -j N 開啟其他 cpu core 來編譯。 EX: make -j 3(for 雙核心)。

Generate environment configuration

Create a new file:

cd ~/crosscompile
vi env.sh

Content:

export STAGING_DIR=/home/maple/crosscompile/openwrt/staging_dir
export TOOLCHAIN_DIR=$STAGING_DIR/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2
export LDCFLAGS=$TOOLCHAIN_DIR/usr/lib
export LD_LIBRARY_PATH=$TOOLCHAIN_DIR/usr/lib
export PATH=$TOOLCHAIN_DIR/bin:$PATH

Change value of STAGING_DIR by the path of OpenWRT source code

source env.sh

每一次 login 都要作這個步驟,因此有兩種(懶人)方法可以簡化這個步驟

  1. echo 'source ~/crosscompile/env.sh' >> ~/.bashrc
  2. cat ~/crosscompile/env.sh | sudo tee -a /etc/profile or sudo mv ~/crosscompile/env.sh /etc/profile.d/

之後登入就會自動讀取 env.sh 裡面的環境變數了

Cross compile

Compile a software package

You need activate environment parameter before this step

cd ~/crosscompile
wget -O- http://downloads.openwrt.org/sources/bluez-libs-3.36.tar.gz | tar zxf -
cd bluez-libs-3.36
./configure --prefix=$TOOLCHAIN_DIR --build=mips-openwrt-linux-gnu --host=mips-openwrt-linux-uclibc
make
make install

Hello world

cd ~/crosscompile
mkdir helloworld
cd helloworld
vi helloworld.c
#include <stdio.h>

int main() {
    printf("Hello World!\n");
    return 0;
}

Compile single source code

mips-openwrt-linux-gcc -o helloworld helloworld.c

Compile with Makefile

INCLUDE_DIR=$(TOOLCHAIN_DIR)/usr/include

CC=mips-openwrt-linux-gcc

CFLAGS= -std=gnu99
LDFLAGS=-lbluetooth

SOURCES=helloworld.c
OBJS=$(SOURCES:.c=.o)

all: helloworld

helloworld.o: helloworld.c
	$(CC) -c $(CFLAGS) -I $(INCLUDE_DIR) -o $@ $<

%.o: %.c %.h
	$(CC) -c $(CFLAGS) -I $(INCLUDE_DIR) -o $@ $<

helloworld: $(OBJS)
	$(CC) $(LDFLAGS) $(CFLAGS) -o helloworld $(OBJS)

clean:
	rm *.o helloworld

縮排必須是一格 tab,不能是空白鍵,否則會產生 Makefile:17: *** missing separator. Stop. 錯誤。

完成

Sniffex

Install libpcap

You need activate environment parameter before this step

cd ~/crosscompile
wget -O- http://downloads.openwrt.org/sources/libpcap-1.5.3.tar.gz | tar zxf -
cd libpcap-1.5.3
./configure --prefix=$TOOLCHAIN_DIR --build=mips-openwrt-linux-gnu --host=mips-openwrt-linux-uclibc --with-pcap=null
make
make install

Compile sniffex

sudo apt-get install m4 flex bison
cd ~/crosscompile
mkdir sniffex
wget http://www.tcpdump.org/sniffex.c
INCLUDE_DIR=$(TOOLCHAIN_DIR)/usr/include

CC=mips-openwrt-linux-gcc

CFLAGS= -Wall
LDFLAGS=-lpcap

SOURCES=sniffex.c
OBJS=$(SOURCES:.c=.o)

all: sniffex

sniffex.o: sniffex.c
	$(CC) -c $(CFLAGS) -I $(INCLUDE_DIR) -o $@ $<

%.o: %.c %.h
	$(CC) -c $(CFLAGS) -I $(INCLUDE_DIR) -o $@ $<

sniffex: $(OBJS)
	$(CC) $(LDFLAGS) $(CFLAGS) -o sniffex $(OBJS)

clean:
	rm *.o sniffex

Done

Reference

相關文章