概要描述
为了实现在路由器上运行我们的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的空格以及设置问题,
或者找我要其他的样例代码进行测试。