Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1485306
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: LINUX

2013-02-17 09:02:26

如果想更快捷的调试代码, 可以利用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# 
阅读(7450) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~