编译一个可以运行在openwrt上的c程序
2015-07-16 10:27
609 查看
转载地址:http://aboutchen.org/blog/compile-c-code-for-openwrt/
本文的测试环境为
首先在ubuntu上安装一些东西
获取Openwrt的源代码
编译源代码,默认配置即可,不需要勾选什么
编译,耗时较长,可以忙些别的事情
一切无错的情况下,会在
将
编译
直接在本机运行的话会报如下错误
因为是为MIPS指令架构编译的,所以如果想在本机上运行,可以使用之前安装的
如果要编译MIPS64架构,只需要编译的时候加个参数
使用file命令可以看下可执行文件的信息
使用SCP上传到路由器,运行即可.
本文的测试环境为
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上传到路由器,运行即可.
相关文章推荐
- Linux C函数参考手册(PDF版)
- Lua教程(十七):C API简介
- C#实现打造气泡屏幕保护效果
- C/C++数据对齐详细解析
- C 语言基础教程(我的C之旅开始了)[三]
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- C字符串操作函数实现方法小结
- C/C++ 宏详细解析
- C/C++实现对STORM运行信息查看及控制的方法
- 用C#生成不重复的随机数的代码
- 在c和c++中实现函数回调
- 浅析C/C++中被人误解的SIZEOF
- C 语言基础教程(我的C之旅开始了)[六]
- C/C++实现的游戏角色名称名字随机生成代码
- C/C++函数调用的几种方式总结
- c/c++中变量的声明和定义深入解析
- 浅析c与c++中struct的区别
- 深入详解C编写Windows服务程序的五个步骤