openwrt 编译c语言,openwrt之交叉编译

本文介绍了如何利用OpenWrt的交叉编译工具链进行代码调试,通过设置环境变量STAGING_DIR,使用mips-openwrt-linux-gcc编译hello.c程序,并创建Makefile自动化编译过程。编译完成后,将可执行文件复制到路由器上执行,实现快速调试。
摘要由CSDN通过智能技术生成

如果想更快捷的调试代码, 可以利用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#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值