手上有几个硬改刷机的OpenWrt路由,固件都是网上找的现成的,自己也没尝试过搭建编译环境。但一直有个想法,OpenWrt是Linux系统,那么我可以编译个C程序在上面运行,虽没想好用来干啥,但总想试试。
1. 下载工具链
这里找了两篇介绍安装mips编译器和模拟器/mips GCC编译环境搭建和MIPS交叉编译环境的搭建(Buildroot)。抱着试试的心态,我从https://www.uclibc.org/downloads/binaries/0.9.30.1/下载了cross-compiler-mips.tar.bz2 2009-04-11 04:05 20M
。
$ tar -jxf cross-compiler-mips.tar.bz2
$ cd cross-compiler-mips/bin/
$ ./mips-gcc # 即mips的编译器
2. hello word
编写一个hello word测试一下。
main.c
#include <stdio.h> int main(int argc, char *argv[]) { puts("hello word !!!"); return 0; }
$ ./mips-gcc main.c # 使用mips工具链编译
$ ls -l a.out # mips平台的应用程序,可以在mips linux系统下运行
-rwxrwxr-x 1 xflm xflm 5567 5月 4 23:15 a.out
3. 运行
发送到OpenWrt路由上试试看,我这边路由开了ssh,故而可以使用scp传送文件,也可以采用nc
命令传送文件,参考linux下使用nc命令通过网络传送文件。
scp -P 22 a.out root@192.168.10.1:/tmp/a.out # 电脑上使用SCP协议传送文件,会要求输入远程的登录密码
$ cd /tmp # 登录OpenWrt的终端,执行a.out程序
$ chmod +x a.out
$ ./a.out
hello word !!! # 运行成功了