openwrt环境搭建编译demo

简介: openwrt环境搭建编译demo

概要描述

为了实现在路由器上运行我们的demo 模块代码,使用路由器对应的固件openwrt SDK,在linux环境上进行demo环境搭建测试,生成ipk文件,在路由器上安装运行。

准备动作

1:纯净的ubuntu环境,这里我用ubuntu 20进行测试。

2:固件openwrt 对应的SDK包(这里我们使用SDK进行编译,SDK有编译需要用的工具和平台相关的代码等)

开始进行编译:

1:纯净的ubuntu环境,进行更新及必要软件的安装

执行下面相关的命令:

sudo apt-get update
sudo apt-get upgrade
sudo apt install make
sudo apt-get install build-essential
2:安装路由器固件openwrt SDK编译需要的一些依赖包

执行下面相关命令:

sudo apt install build-essential ccache ecj fastjar file g++ gawk gettext git java-propose-classpath libelf-dev libncurses5-dev libncursesw5-dev libssl-dev python python2.7-dev python3 unzip wget python3-distutils python3-setuptools rsync subversion swig time xsltproc zlib1g-dev
3:配置样例代码,开始进行编译
1: 上传SDK包,解压后,查看样例代码结构

注意:这里使用固件 openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64.tar.xz

这里样例代码helloword直接上传到SDK包package目录中

hlp@ubuntu:~/openwrt$ tar -xf openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64.tar.xz
hlp@ubuntu:~/openwrt$ tar -xf helloworld.tar.xz 
hlp@ubuntu:~/openwrt$ ll
drwxr-xr-x 3 hlp hlp     4096 Apr 15 04:46 helloworld/
-rw-rw-r-- 1 hlp hlp     1764 Apr 22 02:35 helloworld.tar.xz
drwxr-xr-x 9 hlp hlp     4096 Dec  8 05:09 openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64/
-rw-rw-r-- 1 hlp hlp 57034908 Apr 22 02:35 openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64.tar.xz
hlp@ubuntu:~/openwrt$ cp -r helloworld /home/hlp/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64/package
hlp@ubuntu:~/openwrt$ cd /home/hlp/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64/package
hlp@ubuntu:~/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64/package$ ls
helloworld  linux  Makefile  toolchain
hlp@ubuntu:~/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64/package$ tree helloworld/
helloworld/
├── Makefile
└── src
    ├── helloworld.c
    └── Makefile
1 directory, 3 files
2: 开始构建编译

执行“make -j V=s package/helloworld/compile” 开始编译

make -j V=s package/helloworld/compile

第一次执行make时,或者执行“make menuconfig”时,会打开SDK菜单,可以禁用一些“默认配置”(会构建每个软件包)。

选择 : Global Build Settings

注意: 我在重新测试的时候发现,有时候编译的时候,这里的选项必须选择上才能成功,具体原理没清楚。

在子菜单中取消下列相关选项:

Select all target specific packages by default = OFF
Select all kernel module packages by default = OFF
Select all userspace packages by default = OFF
Also deselect the Crypto package = OFF

开始编译以及查看编译结果:

hlp@ubuntu:~/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64$ make -j V=s package/helloworld/compile
...
#这里如果编译没有报错,查找对应的ipk文件:
hlp@ubuntu:~/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64$ find -name *.ipk|grep helloworld
./bin/packages/mipsel_24kc/base/helloworld_2.0-2_mipsel_24kc.ipk

可以看到,我们已经生成需要的ipk文件:

helloworld_2.0-2_mipsel_24kc.ipk

3:样例代码以及Makefile相关文件:

1:helloworld/src/helloworld.c

#include <stdio.h>
int main(void)
{
    printf("\nHello, world!\n\n");
    return 0;
}

2:helloworld/src/Makefile

# build helloworld executable when user executes "make"
helloworld: helloworld.o
  $(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
  $(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
  rm *.o helloworld

3:helloworld/Makefile

##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to 
# specify a basic description of the package, 
# where to build our program, where to find 
# the source files, and where to install the 
# compiled program on the router. 
# 
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and 
# there should be no trailing whitespace in
# lines that are not commented.
# 
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_VERSION:=2.0
PKG_RELEASE:=2
# This specifies the directory where we're going to build the program.  
# The root build directory, $(BUILD_DIR), is by default the build_mipsel 
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
include $(INCLUDE_DIR)/package.mk
# Specify package information for this program. 
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION 
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=Helloworld -- prints a snarky message
endef
# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
  If you can't figure out what this program does, you're probably
  brain-dead and need immediate medical attention.
endef
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
  mkdir -p $(PKG_BUILD_DIR)
  $(CP) ./src/* $(PKG_BUILD_DIR)/
endef
# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one
# Specify where and how to install the program. Since we only have one file, 
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running 
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install 
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the 
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
  $(INSTALL_DIR) $(1)/bin
  $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

测试demo

上传到我们的路由器固件环境上,使用opkg 进行安装并执行进行测试。

使用scp命令进行文件的远程拷贝,拷贝到我们的目标机192.168.50.189的/tmp/目录下:

hlp@ubuntu:~/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64$ find -name *.ipk|grep helloworld
./bin/packages/mipsel_24kc/base/helloworld_2.0-2_mipsel_24kc.ipk
hlp@ubuntu:~/openwrt/openwrt-sdk-ramips-mt7621_gcc-7.4.0_musl.Linux-x86_64$ scp ./bin/packages/mipsel_24kc/base/helloworld_2.0-2_mipsel_24kc.ipk root@192.168.50.189:/tmp/
The authenticity of host '192.168.50.189 (192.168.50.189)' can't be established.
RSA key fingerprint is SHA256:+84ZoCRn+MwGF4lgdT7Kq5WJs9yWrb/KK5qPslu0ZAQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.50.189' (RSA) to the list of known hosts.
root@192.168.50.189's password: 
helloworld_2.0-2_mipsel_24kc.ipk

在目标机192.168.50.189环境上进行查看,安装,以及测试:

root@transCOMM:/tmp# ls -l hello*
-rw-r--r--    1 root     root          1862 Apr 11 14:57 helloworld_1.0-1_mipsel_24kc.ipk
-rw-r--r--    1 root     root          1895 Apr 17 17:40 helloworld_2.0-2_mipsel_24kc.ipk
root@transCOMM:/tmp# opkg install helloworld_2.0-2_mipsel_24kc.ipk 
Installing helloworld (2.0-2) to root...
Configuring helloworld.
Collected errors:
 * opkg_conf_parse_file: Duplicate src declaration (openwrt_base http://downloads.openwrt.org/snapshots/packages/mipsel_24kc/base). Skipping.
 * opkg_conf_parse_file: Duplicate src declaration (openwrt_luci http://downloads.openwrt.org/snapshots/packages/mipsel_24kc/luci). Skipping.
 * opkg_conf_parse_file: Duplicate src declaration (openwrt_packages http://downloads.openwrt.org/snapshots/packages/mipsel_24kc/packages). Skipping.
root@transCOMM:/tmp# helloworld
Hello, world!
root@transCOMM:/tmp#

其他:opkg update/opkg remove helloworld

注意:如果编译有问题,可能是makefile有问题,但也可能是其他问题(有不知所以的问题)

同时,登录路由器ip,查看:

如果编译有报错解决不了,请分析Makefile的空格以及设置问题,

或者找我要其他的样例代码进行测试。

目录
相关文章
|
Ubuntu 测试技术 Linux
dpdk测试环境搭建(vmware下ubuntu环境参考上文汇总流程)
dpdk测试环境搭建(vmware下ubuntu环境参考上文汇总流程)
1405 0
|
安全 算法 区块链
花无涯带你走进黑客世界之Tor洋葱网络
Tor本来是为用户提供匿名上网保护用户隐私的工具,但是对于一些用户来说,他们可以利用Tor的隐蔽性进行黑客攻击或非法交易活动。总结Tor的恶意应用主要表现在以下几方面。
1223 0
|
XML 算法 Java
基于雪花算法解决Mybatis-Plus id主键生成的问题
基于雪花算法解决Mybatis-Plus id主键生成的问题
2272 0
|
12月前
|
人工智能 自然语言处理
🔥AI实训营重磅升级!12月RAG专题课上线,打造你的“智能阅读搭子”,学知识、做项目、赢好礼三不误!
还在为PDF文档读不完、记不住而烦恼?阿里云AI实训营全新升级,2025年12月4日-2026年1月5日推出首期RAG专题课!手把手教你用阿里云百炼平台搭建“PDF智能阅读助手”,实现文档解析、语义检索与智能问答。大咖亲授,零基础入门,参与即有机会赢取限量周边和技术礼包,邀请好友还能冲榜赢行李箱!立即加入,玩转AI智能应用开发。
445 8
|
Java Maven Android开发
微服务——SpringBoot使用归纳——Spring Boot开发环境搭建和项目启动
本文介绍了Spring Boot开发环境的搭建和项目启动流程。主要内容包括:jdk的配置(IDEA、STS/eclipse设置方法)、Spring Boot工程的构建方式(IDEA快速构建、官方构建工具start.spring.io使用)、maven配置(本地maven路径与阿里云镜像设置)以及编码配置(IDEA和eclipse中的编码设置)。通过这些步骤,帮助开发者顺利完成Spring Boot项目的初始化和运行准备。
1512 0
微服务——SpringBoot使用归纳——Spring Boot开发环境搭建和项目启动
|
Kubernetes Cloud Native 异构计算
133_云端扩展:Kubernetes scaling - 设置自动缩放的阈值与LLM部署最佳实践
在大语言模型(LLM)部署的时代,如何高效地管理计算资源、应对动态负载并优化成本,成为了每个AI工程师必须面对的挑战。随着LLM应用的普及,用户请求模式变得日益复杂且难以预测,传统的静态资源配置方式已无法满足需求。Kubernetes作为云原生时代的容器编排平台,其强大的自动扩展能力为LLM部署提供了理想的解决方案。
754 2
|
人工智能 自然语言处理 算法
|
存储 Ubuntu Linux
Linux Mint与Ubuntu:它们之间的差异真的很大吗?
因为它们之间的差异主要体现在您与图形用户界面(GUI)的交互方式上,因此我建议您在做出决定之前先试用一下Ubuntu和Linux Mint。您可以使用虚拟机进行试用,或者甚至只需在浏览器中尝试一下。
|
API 开发者 Python
Pygame Zero(pgzrun)详解(简介、使用方法、坐标系、目录结构、语法参数、安装、实例解释)
Pygame Zero(pgzrun)详解(简介、使用方法、坐标系、目录结构、语法参数、安装、实例解释)
3927 17

热门文章

最新文章