Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3398
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2019-09-16 21:21
个人简介

一步一步学习嵌入式系统

文章分类
文章存档

2019年(1)

我的朋友
最近访客

分类: LINUX

2019-09-16 21:34:51

编译一个可以运行在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/bin export PATH=$STAGING_DIR/:$PATH

编译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上传到路由器,运行即可.

阅读(1883) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~