如果想更快捷的调试代码, 可以利用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#
阅读(1603) | 评论(0) | 转发(0) |