编译 OpenWrt 开发环境
文章目录
一、下载OpenWrt
1.1 打开官网
# 浏览器打开网址
https://openwrt.org/
# 选择下载页面
点击左侧 "Downloads"
# 选择 “Source code: Git repositories” 下的git地址跳转至Git页面
点击 "https://git.openwrt.org/"
1.2 选择特定版本
- 选择 “openwrt/openwrt.git”
- 点击 “tags” 查看所有发布版本
- 查看所有已发布版本
-
查看该版本使用的gcc版本(以v18.06.2为例)
点击 "v18.06.2" -> "tree" -> "toolchain" -> "gcc" -> "patches"gcc 版本为 7.3.0
1.3 下载特定版本
-
通过git命令下载(速度较慢,但可使用 ./scripts/feeds update 命令更新)
git clone --branch v18.06.2 https://git.openwrt.org/openwrt/openwrt.git - 浏览器直接下载(速度较快,但不能使用 ./scripts/feeds update 命令更新)
二、编译 OpenWrt 开发环境
2.1 安装说明
You need gcc, binutils, bzip2, flex, python, perl, make, find, grep, diff, unzip,
gawk, getopt, subversion, libz-dev and libc headers installed.
1. Run "./scripts/feeds update -a" to obtain all the latest package definitions
defined in feeds.conf / feeds.conf.default
2. Run "./scripts/feeds install -a" to install symlinks for all obtained
packages into package/feeds/
3. Run "make menuconfig" to select your preferred configuration for the
toolchain, target system & firmware packages.
4. Run "make" to build your firmware. This will download all sources, build
the cross-compile toolchain and then cross-compile the Linux kernel & all
chosen applications for your target system.
5. 执行 update 与 install 时不应产生任何警告信息,如果产生请多次执行上述两个指令消除警告。
6. 替换 git.openwrt.org 为 github 安装源可提高安装包下载速度。
7. 注意:OpenWrt解压至特定目录时不要重命名并执行make命令,此时会删除原构建目录并重新构建
rm -rf ~/$(openwrt_dir)/build_dir/target-mipsel_mips32_musl/linux-brcm47xx_generic
2.2 安装依赖库
# 可通过 make menuconfig 来确定缺少哪些依赖库
apt-get install subversion build-essential libncurses5-dev zlib1g-dev gawk git ccache gettext libssl-dev xsltproc wget unzip python
2.3 更改安装源
# 原始安装源内容 feeds.conf.default
src-git packages https://git.openwrt.org/feed/packages.git^911bbd6bb4856f1e28ae00af37df62e4fa3529e5
src-git luci https://git.openwrt.org/project/luci.git^6f6641d97de2c85ee5d87beda92ae8437d1dbdf5
src-git routing https://git.openwrt.org/feed/routing.git^ea345d16a6e27c2a8fdf67bf543cc36a5f189131
src-git telephony https://git.openwrt.org/feed/telephony.git^cb939d9677d6e38c428f9f297641d07611edeb04
# 替换安装源内容 feeds.conf.default
src-git packages https://github.com/openwrt/packages.git^911bbd6bb4856f1e28ae00af37df62e4fa3529e5
src-git luci https://github.com/openwrt/luci.git^6f6641d97de2c85ee5d87beda92ae8437d1dbdf5
src-git routing https://github.com/openwrt-routing/packages.git^ea345d16a6e27c2a8fdf67bf543cc36a5f189131
src-git telephony https://github.com/openwrt/telephony.git^cb939d9677d6e38c428f9f297641d07611edeb04
2.4 更新和安装可选软件包
# 更新最新的包定义
./scripts/feeds update
# 安装所有的包
./scripts/feeds install -a
# 说明
# a. 通过浏览器下载的压缩包因为不含git相关信息,因此无法使用此步骤。
# b. 如果之前已下载dl目录下的部分文件,直接拷贝dl目录至openwrt根目录,减少下载时间。
2.5 备份选项
# 打开配置菜单
make menuconfig
# save 按钮保存当前配置
# load 按钮加载以前配置
# 默认配置为.config,可用.default作为初始配置,.config作为临时配置
2.6 配置选项
make menuconfig
- 选择CPU架构(Target System BCM47xx/53xx(MIPS))
2.7 编译代码
# 采用2个线程编译,并输出每一步的执行动作及详细的错误信息
make V=s -j2
# 如果产生问题,关闭多线程编译
make V=s
2.8 编译成功
# 编译成功时输出类似如下信息
Generating index for package ./ubusd_2018-10-06-221ce7e7-1_mipsel_mips32.ipk
Generating index for package ./uci_2018-08-11-4c8b4d6e-1_mipsel_mips32.ipk
Generating index for package ./uclient-fetch_2018-11-24-3ba74ebc-1_mipsel_mips32.ipk
Generating index for package ./usign_2015-07-04-ef641914-1_mipsel_mips32.ipk
Generating index for package ./wireless-regdb_2017-10-20-4343d359_mipsel_mips32.ipk
Generating index for package ./wpad-mini_2018-05-21-62566bc2-5_mipsel_mips32.ipk
Signing package index...
make[2]: Leaving directory '/home/clxye/openwrt'
export MAKEFLAGS= ;make -w -r checksum
make[2]: Entering directory '/home/clxye/openwrt'
make[2]: Leaving directory '/home/clxye/openwrt'
make[1]: Leaving directory '/home/clxye/openwrt'
2.9 清除编译
# 清除单个软件包
make package/xxx/clean
# 清除所有软件包
make package/clean
# 清除所有构建(删除build_dir和staging_dir,恢复至初次make之前的状态,慎用)
make clean
三、编译自己的工程依赖库
3.1 确定需要安装的库
我们项目中需要使用HTTP/2通信,通信库采用libcurl,该库的http2部分又依赖于nghttp2库。
因此,我们需要安装`nghttp2`库和`libcurl`库。
3.2 打开软件包选择界面
-
启动 package 选择界面
# 位于openwrt根目录 make menuconfig - 启动后的界面
3.3 搜索指定软件包位置
- 按下‘/’按键进入搜索模式
- 按下'Enter'查看搜索结果
- 由上图分析,我们只需选中“LIBCURL_NGHTTP2"选项即可自动选中“nghttp2”和"libcurl"。
3.4 选中指定软件包
- 通过键盘上下键移动光标至"Libraries --->"并回车
- 通过键盘上下键移动光标至"libcurl",单击空格键选中并回车
- 选中“HTTP2 protocol”
3.5 保存选项并退出
- 通过键盘左右键移动光标至“保存”并回车
- 单击键盘回车键保存配置
3.6 退出配置菜单
-
多次按下键盘“ESC”,直至退出软件包配置主菜单
*** End of the configuration. *** Execute 'make' to start the build or try 'make help'. clxye@clxye:~/openwrt$
3.7 重新编译
-
执行“make”命令
# 此时会编译nghttp2和libcurl软件包 make -
运行结果
make[3] -C package/network/utils/iw compile make[3] -C package/kernel/mac80211 compile make[3] -C package/libs/mbedtls compile make[3] -C package/libs/nghttp2 compile
四、常见问题
4.1 Can’t locate Getopt/Std.pm in INC
# 获取本地当前perl版本
# perl -v
This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux
# 下载本机当前perl版本对应的源码重新编译
http://www.cpan.org/src/README.html
# 解压源码后编译安装
# ./Configure -des -Dprefix=/usr
# make && make install
4.2 set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check
export set FORCE_UNSAFE_CONFIGURE=1
4.3 ./scripts/feeds update 权限不够
chmod +x scripts/feeds
4.4 Your display is too small to run Menuconfig!
# 调整控制台窗口尺寸(例如:最大化)
4.5 bash: ./files/pywrap.sh: Permission denied
chmod +x tools/scons/files/pywrap.sh
4.6 configure: error: Your intltool is too old. You need intltool 0.35.0 or later.
wget http://ftp.gnome.org/pub/gnome/sources/intltool/0.40/intltool-0.40.6.tar.gz
tar -zxvf intltool-0.40.6.tar.gz
cd intltool-0.40.6
./configure
make
make install
4.7 error: XML::Parser perl module is required for intltool
sudo apt-get install libxml-parser-perl
4.8 如何设置为默认DHCP获取IP地址
# 修改 /package/base-files/files/bin/config_generate 文件
106 行
set network.$1.proto='static'
修改为
set network.$1.proto='dhcp'

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论