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

本文的测试环境为 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上传到路由器,运行即可.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值