OpenWRT 学习

零散知识

1、快速编译小程序

编写helloworld程序

#include <stdio.h>
int main(char argc, char *argv[])
{
    int i = 0;
    while(1){
        printf("Hello world!!!%d\n",i++); //打印内容
        sleep(1);// 一秒钟打印一次
    }
    return 0;
}

使用交叉编译工具进行编译

把源文件helloworld.c拷贝到OpenWRT源码交叉编译工具目录

staging_dir/toolchain-aarch64_generic_gcc-7.4.0_musl/bin/

加粗字体根据平台不同可能不一样,不在交叉编译目录编译可能会有依赖报错

mipsel-openwrt-linux-gcc helloworld.c -o helloworld

使用scp拷贝到目标板

最好放tmp目录,~/目录找不到

scp helloworld root@172.16.2.104:/tmp

进入目标板执行程序

root@router:/tmp#./helloworld
Hello world!!!0
Hello world!!!1
Hello world!!!2
Hello world!!!3

2、使用Makefile添加到系统中

目录结构

/openwrt/package/helloworld$ tree
.
├── Makefile
└── src
    ├── helloworld.c
    └── Makefile

主Makefile

include $(TOPDIR)/rules.mk
 
PKG_NAME:=helloworld
PKG_RELEASE:=1
 
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
 
include $(INCLUDE_DIR)/package.mk
 
define Package/helloworld
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:=Helloworld -- prints a snarky message
endef
 
define Package/helloworld/description
    It's my first package demo.
endef
define Build/Prepare
    echo "Here is Package/Prepare"
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/helloworld/install
    echo "Here is Package/install"
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
$(eval $(call BuildPackage,helloworld))

副Makefile

helloworld : helloworld.o
    $(CC) $(LDFLAGS) helloworld.o -o helloworld
 
helloworld.o : helloworld.c
    $(CC) $(CFLAGS) -c helloworld.c
 
clean :
    rm *.o helloworld

编译

将代码编写完成后,就通过make menuconfig找到helloworld所在的位置,helloworld的位置在主Makefile里面有定义​​CATEGORY:=Utilities

 之后make V=s编译

运行

直接copy程序

可以直接将编译好的helloworld 通过scp拷贝到目标板

通过ipk包安装

在编译好的源码中搜索安装包helloworld_1_ramips_24kec.ipk,传入到目标板后安装

opkg install helloworld_1_ramips_24kec.ipk

刷机安装

如果刷机,进入系统/bin目录可以看到应用程序helloworld

3、刷机

通过网线连接目标板和自己的pc后,用浏览器访问目标板ip,就可以进入管理界面,输入用户名密码(密码一般是admin)登录后,点击菜单栏  系统->备份/升级进入升级界面。在选择文件处选择编译好的bin文件:code/bin/targets/ramips/mt7628/lede-ramips-mt7628-zbt-we33-sysupgrade.bin
点击刷写固件进行升级

常见刷写失败原因:

1、主机型号不匹配

make menuconfig,将devices profile改成正确的。我就遇到过,改成WE3326后就可以了

4、 开启usb host功能

1. 参考下面文章,通过make menuconfig 开启usb的几个选项,插入ush后,查看dmesg,已经有事件上来。

Openwrt开发指南 第24章 配置开发板支持U盘-电子发烧友网 (elecfans.com)

Kernel modules --->
        USB Support --->
                kmod-usb-core
                kmod-usb-hid
                kmod-usb-ohci
                kmod-usb-uhci
                kmod-usb2

2. 验证功能

1.通过dmesg可以查看到usb插入的事件的log

2.进入/dev/input/  可看到event0结点,cat event0,移动鼠标或按下键盘的按键,会有事件上报。

5、直接调用动态库内部API(将动态库link到应用程序)

如果有依赖的动态库,以第一节的helloworld为例,假设依赖的so名字为libtest.so。
1、首先把libtest.so放到src目录
2、修改主Makefile:
        在define Package/helloworld里添加DEPENDS:=+libstdcpp +libpthread
        在define Package/helloworld/install里添加
                    $(INSTALL_DIR) $(1)/usr/lib
                    $(CP) $(PKG_BUILD_DIR)/lib*.so $(1)/usr/lib/
最终的主Makefile如下:

include $(TOPDIR)/rules.mk
 
PKG_NAME:=helloworld
PKG_RELEASE:=1
 
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
 
include $(INCLUDE_DIR)/package.mk
 
define Package/helloworld
    SECTION:=utils
    CATEGORY:=Utilities
    DEPENDS:=+libstdcpp +libpthread
    TITLE:=Helloworld -- prints a snarky message
endef
 
define Package/helloworld/description
    It's my first package demo.
endef
define Build/Prepare
    echo "Here is Package/Prepare"
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef
define Package/helloworld/install
    echo "Here is Package/install"
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
    $(INSTALL_DIR) $(1)/usr/lib
    $(CP) $(PKG_BUILD_DIR)/lib*.so $(1)/usr/lib/
endef
$(eval $(call BuildPackage,helloworld))

3、 修改副Makefile
在开头添加LDFLAGS += libtest.so
修改后的完整Makefilefile如下

LDFLAGS += libtest.so
helloworld : helloworld.o
    $(CC) $(LDFLAGS) helloworld.o -o helloworld
 
helloworld.o : helloworld.c
    $(CC) $(CFLAGS) -c helloworld.c
 
clean :
    rm *.o helloworld

参考文档

6-Openwrt SDK-CSDN博客

MT7688学习笔记(3)——定制OpenWrt系统及添加自开发软件 (cdsy.xyz)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值