您的位置:首页 > 运维架构

编译一个可以运行在openwrt上的c程序

2015-07-16 10:27 609 查看
转载地址:http://aboutchen.org/blog/compile-c-code-for-openwrt/

本文的测试环境为 
Ubuntu 14.0
 
TP-LINK WDR4320
 
OpenWrt Attitude Adjustment 12.09


首先在ubuntu上安装一些东西

sudo apt-get install libncurses5-dev gawk flex build-essential qemu-user


获取Openwrt的源代码

git clone git://git.openwrt.org/openwrt.git


编译源代码,默认配置即可,不需要勾选什么

cd openwrt
make menuconfig


编译,耗时较长,可以忙些别的事情

nice make -j16


一切无错的情况下,会在
staging_dir
目录下列出三个目录
host

target-mips_34kc_uClibc-0.9.33.2

toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2


toolchain
加到
PATH
里面

export STAGING_DIR=/path/to/openwrt/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2
export PATH=$PATH:$STAGING_DIR/bin


编译
hello.c


mips-openwrt-linux-gcc hello.c -o hello -static


直接在本机运行的话会报如下错误

shmiluyu@:~/openwrt/src$ ./hello
bash: ./hello: cannot execute binary file: 可执行文件格式错误


因为是为MIPS指令架构编译的,所以如果想在本机上运行,可以使用之前安装的
QEMU


shmiluyu@:~/openwrt/src$ qemu-mips hello
hello openwrt


如果要编译MIPS64架构,只需要编译的时候加个参数

mips-openwrt-linux-gcc hello.c -o hello-mips64 -static -mips64r2


使用file命令可以看下可执行文件的信息

shmiluyu@:~/openwrt/src$ file hello
hello: ELF 32-bit MSB  executable, MIPS, MIPS32 rel2 version 1, statically linked, not stripped


使用SCP上传到路由器,运行即可.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  openwrt c wrtnode