给路由器安装好Openwrt系统后,在上边运行自己编写的程序,一般需要在宿主机器上交叉编译成mips架构的二进制程序,然后拷贝到路由器上运行。
1. 搭建交叉编译环境
交叉编译需要交叉编译工具链,在编译Openwrt时。交叉编译工具链以及生成了,我们只需要将路径添加到系统环境变量中去。
exportSTAGING_DIR=/path/to/openwrt/trunk/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/
exportPATH=$PATH:$STAGING_DIR/bin
其中STAGING_DIR每个人的路径是不一样的,需要根据自己情况更改。
2. Hello Word程序
新建一个test.c文件,添加
#include<stdio.h>
voidmain()
{
printf("hello world\n");
return;
}
在终端输入编译命令:
./mipsel-openwrt-linux-gcc test.c -o test
最后生成test程序,通过scp或者其他手段拷贝到路由器,在路由器上运行,输出
root@OpenWrt:~# ./test
hello world