Chinaunix首页 | 论坛 | 博客
  • 博客访问: 173521
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-07 15:33
文章存档

2015年(23)

2014年(10)

分类: LINUX

2014-10-07 15:36:20

原文地址:openwrt之交叉编译 作者:qiushui_007

如果想更快捷的调试代码, 可以利用openwrt的交叉编译

1. shell中如下命令
export STAGING_DIR="~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir"

2. hello.c, 代码如下:
/*------------------------------------------------------------------------------------------
openwrt之交叉编译
~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc hello.c -o hello

如果直接在虚拟机上执行, 则错误如下
xxg@xxg-desktop:~/1-wire/helloword$ ./hello
bash: ./hello: cannot execute binary file

路由器上执行
chmod +x hello
./hello
-----------------------------------------------------------------------------------------*/

#include

int main(int agrc, char *argv)
{
        printf ("oepnmips-openwrt-linux-gcc hello.c -o hello\n");
        return 1;
}


3. Makefile, 内容如下
OPENWRT = 1

ifeq ($(OPENWRT), 1)
CC = ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
CFLAGS += -L/home/xxg/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/target-mips_r2_uClibc-0.9.33.2/usr/lib

else
CC = gcc
endif

CFLAGS += -Wall -g

#可执行文件名和相关的obj文件
APP_BINARY = hello
OBJ = hello.o

all: APP_FILE

%.o: %.c
        $(CC) $(CFLAGS) -c -o $@ $<

APP_FILE: $(OBJ)
        $(CC) $(CFLAGS) $(OBJ) -o $(APP_BINARY)        $(LFLAGS)

clean:
        @echo "cleanning project"
        $(RM) *.a *.o *~ *.so *.lo $(APP_BINARY)
        @echo "clean completed"

.PHONY: clean

执行make则编译出 hello 可执行文件, 可以用winscp拷贝到路由器上. 

4. 假设拷贝到路由器的 xutest目录下, 
root@OpenWrt:/xutest# chmod +x hello
root@OpenWrt:/xutest# ./hello
mips-openwrt-linux-gcc hello.c -o hello
root@OpenWrt:/xutest# 
阅读(1564) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~