Cross-compile for OpenWRT
這是一個實驗,在 Ubuntu 上編譯可以在 OpenWRT 上運行的程式。 參考並修改別人的方式,完成了幾個簡單的範例。
- 編譯環境:
- Operating System: Ubuntu 14.04 x64
- OpenWRT version: 14.07
Download OpenWRT source code
cd ~
mkdir ~/crosscompile
git clone git://git.openwrt.org/14.07/openwrt.git
Build OpenWRT
cd openwrt
make menuconfig
- Choice “Target System” and “Target Profile”
- “Advanced configuration options (for developers)” -> “Target Options”
- “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 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 environment
source env.sh
每一次 login 都要作這個步驟,因此有兩種(懶人)方法可以簡化這個步驟
echo 'source ~/crosscompile/env.sh' >> ~/.bashrc
cat ~/crosscompile/env.sh | sudo tee -a /etc/profile
orsudo 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
- Generate source code
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
- Generate
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.
錯誤。
- Execute
make
command (You need activate environment parameter before this step)
完成
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
- Install packages
sudo apt-get install m4 flex bison
- Create source code directory
cd ~/crosscompile
mkdir sniffex
- Get source code
wget http://www.tcpdump.org/sniffex.c
- Generate
Makefile
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
- Execute
make
command (You need source environment parameter before this step)
Done
Reference
- Cross Compiling For OpenWRT On Linux
- missing separator. Stop.
- [Embedded] How to Cross Compile tcpdump
- 如何將tcpdump移植到arm嵌入式系統