主题 : 如何编译OpenWrt 复制链接 | 浏览器收藏 | 打印
疯狂的嵌入式爱好者
级别: 论坛版主
UID: 40543
精华: 1
发帖: 169
金钱: 920 两
威望: 184 点
贡献值: 1 点
综合积分: 358 分
注册时间: 2011-03-19
最后登录: 2013-05-10
楼主  发表于: 2011-08-12 15:25

 如何编译OpenWrt

管理提醒: 本帖被 qq2440 从 友善之臂中国开发者挑战赛 移动到本区(2011-10-13)

 Openwrt 官方正式的发行版是已编译好了的映像文件(后缀名bin或trx、trx2),此映像文件可从Openwrt官方网站的下载页面中轻松获取到,连接地址为 OpenWrt官方网站。

为了参与测试或参与开发OpenWrt的工作;我们需要重新移植或者重新编译个一个openwrt
 若想实现上述目的,其实很简单,按下述文字即可成功编译出一个您的Openwrt来。
准备工作
在开始编译Openwrt之前需要您做些准备工作;与其他编译过程一样,类似的编译工具和编译环境是必不可少的:
一个构建并编译OpenWrt映像的系统,简单说就是准备一个Linux或者类Unix操作系统(比如Ubuntu、Debian,MAC OS,FreeBSD等);
确保安装了所需的依赖关系库, (在debian系统中就是安装各种需要的软件包)
获取到OpenWrt源代码副本
首先, 开机登陆到开发机的操作系统(废话了)。不管是独立安装的或者是运行在虚拟机上的(Vmware 或者 Qemu),推荐使用Linux系统。 在 *BSD 或者是 Max OSX 系统上编译Openwrt也可行,但并不能保证可以成功编译。下文假定您使用的是Debian操作系统,使用 apt-get 来管理包. 您也可以使用其他linux发行版,比如 Ubuntu (分支 Kubuntu, Xubuntu 等即可)。
不过,在虚拟机中编译Openwrt会产生无法预料的问题,所以建议您还是使用物理主机来编译以减少不必要的麻烦。
第二步, 就是安装所需要的各种软件包, 包括编译器,解压工具,特定的库等. 这些工作可以简单的通过键入以下命令 (通常需要root 或者是 sudo 权限),以root权限安装下列软件包(可能并不完整,会有提示,提示缺少即装就可以了):
32位(x86)请执行下列命令:
# apt-get install build-essential asciidoc binutils bzip2 gawk gettext \
  git libncurses5-dev libz-dev patch unzip zlib1g-dev

64位(x86_64)请执行下列命令(多装了哪些库或软件包呢?请您仔细看一看哦):
# apt-get install build-essential asciidoc binutils bzip2 gawk gettext \
  git libncurses5-dev libz-dev patch unzip zlib1g-dev ia32-libs \
  lib32gcc1 libc6-dev-i386


某些依赖的为库或软件包也许操作系统中已经安装过,此时apt-get会作出提示(提示您忽略或重新安装的),别紧张,放轻松些,编译Openwrt不会像编译DD-WRT那样难的(至少本人是体会到了编译DD-WRT的难)。
最后下载一份完整的 Openwrt 源码到编译环境中。关于Openwrt的源代码下载,途径有二,一是通过 subversion ,一是通过 git,建议使用 subversion(SVN) ,因为Openwrt主要以 svn 来维护Openwrt系统的版本。另外,请注意Openwrt中不同的分支版本,一个是用得较多的开发快照,俗称 trunk,二是稳定版,截至至本文截稿前的最新版本是backfire 10.03.1 rc5。
安装Subversion
若你想通过svn下载源代码,你需安装 Subversion。Subversion,或称SVN, 是OpenWrt的project中用来控制版本的系统,它非常类似的 CVS的界面和使用条款。 执行下述命令即可安装SVN,很容易的:
# apt-get install subversion

Subversion安装完毕,通过SVN命令可获取得到一份OpenWrt纯净源代码。您还得创建一个目录以便存放获取得到的Openwrt源代码,要获取源代码你还得输入subversion命令来获取 (svn里这种操作称之为'check out') 。命令很简单的,继续看下去就能见到了,别着急,耐心点儿。
编译流程
编译Openwrt需要以一下五个步骤:
通过Subversion命令获得源代码;
更新(或安装) 扩展软件包,即package feeds;
创建一个默认配置以检查编译环境是否搭建好了 (假如需要的话);
用Menuconfig来配置即将编译生成的固件映像文件的配置项;
最后开始编译固件;
下载源代码
最后,下载一份完整的OpenWrt源代码。你可选择:
下载稳定发行版,或
下载开发版 (俗称"trunk"版)。
使用发行版的源码
截止本文时, Openwrt公开发行的稳定版为 OpenWrt 10.03 "backfire"。此版本是最稳定的,但也许不包括最新更新的补丁或最新编写的出的新功能。
下述代码即举例说明了通过svn从brandkfire获得backfire源代码(此版本意思是从trunk分支的补丁也在backfire版本中了,即包含修复补丁):
# mkdir OpenWrt/
# cd OpenWrt/
# svn co svn://svn.openwrt.org/openwrt/branches/backfire

注解: 上述svn命令将在当前目录创建一个 OpenWrt/backfire/ 子目录,此目录包含此命令获取到的源代码。
您也可以通过下述命令,下载不含修复补丁的backfire的原版源码:
# svn co svn://svn.openwrt.org/openwrt/tags/backfire_10.03

使用开发版源代码
当前的开发版本分支(trunk)已包含最新的实验补丁。此分支或许还突破了Openwrt原来所不支持的硬件设备的限制哦,惊喜的同时也有风险存在。因此,编译trunk版,慎之~
# mkdir OpenWrt/
# cd OpenWrt/
# svn co svn://svn.openwrt.org/openwrt/trunk/

更多详细资料详见: https://dev.openwrt.org/wiki/GetSource.
跟进并更新源代码
因Openwrt的源代码随时都会变动,故此命令将确保您所获取得到的源码的最新性。下述假设您用的是backfire版本的源码:
## Here, backfire is the directory name of the current release branch you're tracking
# cd OpenWrt/backfire/
# svn up

'svn up' 命令用于更新SVN上更新了,但本地尚未更新的这部分源代码(本人实践证明此命令会将本地源码与SVN上的源码先比较,若SVN有更新才会下载更新的部分,很实用的一个命令)。如果未指定目标路径,则此命令将更新当前目录及当前目录的子目录内的源码。
更新软件包
Feeds是一个openwrt的附加软件包管理器
'packages' - many additional basic router features,
'LuCI' - the default www GUI for OpenWrt, and
'Xwrt' - alternative GUI.
In most typical case, you needs at least 'packages' and 'LuCI'.
Before downloading feeds (additional predefined package build recipies for OpenWrt) you can check which feeds sources you want to include. Just edit the file 'feeds.conf.default' in the base directory of your build environment.
Then start the download with
# ./scripts/feeds update -a

After this the downloaded packages need to be installed. The installation step means they will be available during the 'make menuconfig' step later. Without this 'installation' step the packages will not be selectable in 'make menuconfig'.
# ./scripts/feeds install -a

The feeds configuration file makes it easy to add new or experimental packages sources to the build system. Just edit the file and run the update command.
Note: Please note that this step replaces the old method of creating symlinks, which can be still found on-line in many old forum and user-group entries.
Updating Feeds
Just like the basic source code, you need to regularly update the feeds sources. That is done with the same commands:
# ./scripts/feeds update -a
# ./scripts/feeds install -a

Note: If you know that you do not want to add possible new packages for menuconfig, you may skip the install step, when updating already existing feeds.
生成配置
You may not have to make configration always after updating sources and feeds, but making it ensures that all packages from source and feeds are correctly included in your build configuration.
Defconfig
下一步是检查编译环境,若可进行编译则生成默认配置:
# make defconfig

若defconfig回显提示缺少软件包或编译库等依赖,则按提示安装所缺软件包或库等即可,不难的,细心点就行。
Menuconfig
Menuconfig is a text-based tool which handles the selection of target, packages to include in the build, kernel options etc.
# make menuconfig

This will update the dependencies of your existing configuration automatically and you can now proceed to build your updated images.
The intention with the development of 'menuconfig' has been to create a simple yet powerful environment for the configuration of individual OpenWrt images. Menuconfig is more or less self-explanatory, and even the most specialized configuration needs can be solved with its help. Depending on the the particular target platform, package requirements and kernel module needs, the standard process of configuration will include modifying:
Target system
Package selection
Build system settings
Kernel modules
Target system is selected from the extensive list of supported platforms, with the numerous target profiles – ranging from specific devices to generic profiles, all depending on the particular device at hand. Package selection has the option of either 'selecting all package', which might be un-practical in certain situation, or relying on the default set of packages will be adequate or make an individual selection. It is here needed to mention that some package combinations might break the build process, so it can take some experimentation before the expected result is reached. Added to this, the OpenWrt developers are themselves only maintaining a smaller set of packages – which includes all default packages – but, the feeds-script makes it very simple to handle a locally maintained set of packages and integrate them in the build-process.
If you want to use LuCI, you'll need to go to the Administration menu, then LuCI Components sub-menu, and select the first 3 packages: luci-admin-core, luci-admin-full, and luci-admin-mini.
If you don't need PPP, you can remove it by deselecting it under the Network menu.
Using Menuaconfig: Make sure you mark these packages as a '*' instead of just an 'M'.
If you mark with a '*', the package will be built into the firmware you create.
If you mark it with just an 'M', the package will be built but not installed.
The final step before the process of compiling the intended image(s) is to exit 'menuconfig' – this also includes the option to save a specific configuration or load an already existing, and pre-configured, version.
Exit and save.
Source Mirrors
The 'Build system settings' include some efficient options for changing package locations which makes it easy to handle a local package set:
Local mirror for source packages
Download folder
In the case of the first option, you simply enter a full URL to the web or ftp server on which the package sources are hosted. Download folder would in the same way be the path to a local folder on the build system (or network). If you have a web/ftp-server hosting the tarballs, the OpenWrt build system will try this one before trying to download from the location(s) mentioned in the Makefiles . Similar if a local 'download folder', residing on the build system, has been specified. The 'Kernel modules' option is required if you need specific (non-standard) drivers and so forth – this would typically be things like modules for USB or particular network interface drivers etc.
编译固件
万事具备,只欠东风,通过下面简单的make命令来编译:
# make

在多核电脑中编译
具有多核CPU处理器的电脑进行编译,使用下述参数可令编译过程加速。 常规用法为 <您cpu处理器的数目 + 1> – 例如使用3进程来编译 (即双核CPU), 命令及参数如下:
# make -j 3

后台编译
If you intend to use your system while building, you can have the build process only use idle I/O and CPU capacity like this (dualcore CPU):
# ionice -c 3 nice -n 20 make -j 2

编译简单的基本的软件包
When developing or packaging software for OpenWrt, it is convenient to be able to build only the package in question (e.g. with package cups):
# make package/cups/compile V=99

For a package in the feeds it looks like this:
# make package/feeds/packages/ndyndns/compile V=99

编译错误
如果因某种不知道的原因而编译失败,下面有种简单的方法来得知编译到底错在哪里了:
# make V=99 2>&1 |tee build.log |grep -i error

The above saves a full verbose copy of the build output (with stdout piped to stderr) in build.log and only shows errors on the screen.
举例说明:
# ionice -c 3 nice -n 20 make -j 2 V=99 CONFIG_DEBUG_SECTION_MISMATCH=y 2>&1 \
  |tee build.log |egrep -i '(warn|error)'

The above saves a full verbose copy of the build output (with stdout piped to stderr) in build.log and outputs only warnings and errors while building using only background resources on a dual core CPU.
一键编译
即使用脚本来编译Openwrt固件。许多朋友编译Openwrt是用的脚本来编译的,详见: https://forum.openwrt.org/viewtopic.php?id=28267
生成的固件在哪
编译成功后所生成的固件文件位于bin目录下,可用如下命令查看:
# cd bin/
# ls */

Cleaning Up
You might need to clean your build environment every now and then. The following make targets are useful for that job:
Clean
Cleaning the trunk/ directory after the build process is simple and only requires 'make clean'. It deletes contents of 'bin' and 'build_dir' directories.
## See CAUTION below
# make clean

CAUTION: Make sure to move your compiled OpenWrt images to another location before issuing this command since it erases the contents of the "bin" directory (where the compiled OpenWrt images live).
Dirclean
If you want to clean in addition to the build directories also the cross-compile tools (in toolchain directory), use the 'dirclean' make, which does the same as 'clean' and also deletes 'staging_dir' and 'toolchain' contents. 'Dirclean' is your basic "Full clean" operation.
## See CAUTION below
# make dirclean

CAUTION: Make sure to move your compiled OpenWrt images to another location before issuing this command.
Distclean
If complete cleaning of the build environment is required, then all it takes is 'make distclean'. It practically nukes everything you have compiled or configured and also deletes all downloaded feeds contents and package sources. Making distclean means quite a lot downloading afterwards, so it should not be used lightly. 'Dirclean' is enough for most cases.
## See CAUTION below
# make distclean

CAUTION: In addition to all else, this will erase your build configuration (.config), your toolchain and all other sources. Use with care!
There are numerous other functionalities in the OpenWrt build system, but the above should have covered some of the fundamentals.
Installing OpenWrt
In comparison to the actual building of the desired image(s), the installation of the OpenWrt image can sometimes be a little, or more, of a challenge. It can be as simple as using the tools supplied by the manufacturer of the hardware (web interface or recovery tools), over manually uploading a new firmware via tftp and rebooting, to doing extensive hardware modifications with the intent of enabling JTAG access. Though, generally installation is made possible via the bootloader (Redboot, Uboot etc.) or simple copying onto a CompactFlash-card – all depending on the specific device.
See http://wiki.openwrt.org/doc/howto/installing for the specifics.

别人转角遇见爱,我转角遇见猫T _T .
级别: 风云使者
UID: 36096
精华: 3
发帖: 2513
金钱: 14075 两
威望: 2815 点
贡献值: 3 点
综合积分: 5086 分
注册时间: 2011-01-11
最后登录: 2015-11-15
1楼  发表于: 2011-08-12 17:56
这个半英文的看着不爽……
极度缺钱,求捐赠……支付宝兼邮箱:huming2207@qq.com
级别: 侠客
UID: 35779
精华: 0
发帖: 52
金钱: 260 两
威望: 52 点
贡献值: 0 点
综合积分: 104 分
注册时间: 2011-01-06
最后登录: 2013-12-30
2楼  发表于: 2013-02-19 09:29
make menuconfig 针对mini6410的配置有详细的参考图片吗?
imag选择哪种类型比较好
我就想用mini6410实现路由器的功能
级别: 新手上路
UID: 70008
精华: 0
发帖: 24
金钱: 120 两
威望: 24 点
贡献值: 0 点
综合积分: 48 分
注册时间: 2012-05-14
最后登录: 2018-02-08
3楼  发表于: 2014-08-07 11:32
收藏了!!!!感谢前辈