1 下载busybox
下载最新版本busybox源码,下载地址:http://www.busybox.net/downloads/。
本文以当前编写最新版本1.22.1为例,所以下载压缩包名为:busybox-1.22.1.tar.bz2,大概2.1M大小。
2 复制并解压源码包
$ mkdir ~/busybox # 创建工作目录
$ cp busybox-1.22.1.tar.bz2 ~/busybox # 复制源码包
$ cd ~/busybox/ # 进入工作目录
$ tar -jxvf busybox-1.22.1.tar.bz2 # 解压
$ cd busybox-1.22.1/ # 进入解压包
3 配置编译选项
(1)创建安装目录并设置环境
$ mkdir ../install #创建安装目录
$ export PATH=$PATH:/usr/local/arm-2010q1/bin #交叉编译工具路径加入系统环境变量!
然后,需要调用图形界面来设置来配置编译选项,这些选项包括:
交叉编译器前缀,如:arm-none-linux-gnueabi-;
静态编译,建议使用静态编译;
/usr目录,为减少复杂性,一般建议再将应用添加到到/usr目录;
安装目录,上一步的install目录,要使用绝对路径。
$ make menuconfig
(2)交叉编译器前缀
Busybox Settings ---->
Build Options ---->
() Cross Compiler prefix
回车,在弹出的界面中输入交叉编译工具链的前缀:
提示:按Ctrl + ←Backspace(退格键) 可删除已存在的内容。
(3)静态编译
Busybox Settings ---->
Build Options ---->
[*] Build BusyBox as a static binary (no shared libs)
(4)不安装到/usr目录
Busybox Settings --->
General Configuration --->
[*] Don't use /usr
(5)安装目录
Busybox Settings --->
Installation Options ("make install" behavior) --->
BusyBox installation prefix
回车,在弹出的界面中输入安装目录:
最后按【ESC】,提示保存时保存。
提示,以上(1)~(5)步骤后其实是配置了.config文件,上面的步骤也可以通过下面几个命令快速实现:
$ make defconfig #默认配置,执行后生成.config文件,或者allyesconfig使用所有功能
$ sed -e "s:.*CONFIG_CROSS_COMPILER_PREFIX.*:CONFIG_CROSS_COMPILER_PREFIX=\"arm-none-linux-gnueabi-\":" -i .config
$ sed -e 's/.*CONFIG_STATIC.*/CONFIG_STATIC=y/' -i .config
$ sed -e 's/.*CONFIG_INSTALL_NO_USR.*/CONFIG_INSTALL_NO_USR=y/' -i .config
$ sed -e "s:.*CONFIG_PREFIX.*:CONFIG_PREFIX=\"\/home\/veryarm\/busybox\/install\":" -i .config
4 编译安装
$ make # 编译,如果是多核CPU,如四核,使用make -j4可提高编译速度
$ make install # 安装
5 打包复制
$ cd ../install
$ tar -cjvf busybox.tar.bz2 *
将打包后的 busybox.tar.bz2 压缩包通过U盘、FTP或者NFS等方式拷贝到目标板,并分别解压到/bin和/sbin目录。
$ cd busybox.tar.bz2 ~/
$ cd
$ tar -jxvf busybox.tar.bz2 bin -C / # 将压缩包的bin目录解压到根目录/
$ tar -jxvf busybox.tar.bz2 sbin -C / # 将压缩包的sbin目录解压到根目录/
解压后会自动覆盖原来的/bin和/sbin下面的busybox。
------问题解决:
出现如下错误:
miscutils/nandwrite.c: In function 'nandwrite_main':
miscutils/nandwrite.c:151: error: 'MTD_FILE_MODE_RAW' undeclared (first use in this function)
miscutils/nandwrite.c:151: error: (Each undeclared identifier is reported only once
miscutils/nandwrite.c:151: error: for each function it appears in.)
scripts/Makefile.build:197: recipe for target 'miscutils/nandwrite.o' failed
make[1]: *** [miscutils/nandwrite.o] Error 1
Makefile:742: recipe for target 'miscutils' failed
make: *** [miscutils] Error 2
解决办法:
MTD_FILE_MODE_RAW在/usr/include/mtd/mtd-abi.h中定义,直接在nandwrite.c中包含<mtd/mtd-abi.h>编译仍无法通过。将/usr/include/mtd/mtd-abi.h拷贝到busybox的include文件中,然后包含该头文件:
#include "libbb.h"
#include "mtd-abi.h"
#include <mtd/mtd-user.h>
编译可以通过。
继续make,出现如下错误:
util-linux/blkdiscard.c: In function 'blkdiscard_main':
util-linux/blkdiscard.c:72: error: 'BLKSECDISCARD' undeclared (first use in this function)
util-linux/blkdiscard.c:72: error: (Each undeclared identifier is reported only once
util-linux/blkdiscard.c:72: error: for each function it appears in.)
scripts/Makefile.build:197: recipe for target 'util-linux/blkdiscard.o' failed
make[1]: *** [util-linux/blkdiscard.o] Error 1
Makefile:742: recipe for target 'util-linux' failed
make: *** [util-linux] Error 2
解决办法:
BLKSECDISCARD在/usr/include/linux/fs.h中定义,方法如上所述,将/usr/include/linux/fs.h拷贝到busybox的include文件中,然后修改blkdiscard.c中头文件包含:
修改:#include <linux/fs.h>为
#include "fs.h"
继续make,编译通过,但是在链接的时候出现问题:
networking/lib.a(nslookup.o): In function `print_host':
nslookup.c:(.text.print_host+0x44): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
debianutils/lib.a(mktemp.o): In function `mktemp_main':
mktemp.c:(.text.mktemp_main+0x98): warning: the use of `mktemp' is dangerous, better use `mkstemp'
networking/lib.a(ipcalc.o): In function `ipcalc_main':
ipcalc.c:(.text.ipcalc_main+0x25c): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
libbb/lib.a(inet_common.o): In function `INET_resolve':
inet_common.c:(.text.INET_resolve+0x60): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(inetd.o): In function `reread_config_file':
inetd.c:(.text.reread_config_file+0x230): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(netstat.o): In function `ip_port_str':
netstat.c:(.text.ip_port_str+0x50): warning: Using 'getservbyport' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
util-linux/lib.a(nsenter.o): In function `nsenter_main':
nsenter.c:(.text.nsenter_main+0x1b0): undefined reference to `setns'
collect2: ld returned 1 exit status
Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.
Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"
Makefile:717: recipe for target 'busybox_unstripped' failed
make: *** [busybox_unstripped] Error 1
解决办法:
make menuconfig
Linux System Utilities--->nsenter,去掉该选项,重新编译make,又出现如下错误:
networking/lib.a(nslookup.o): In function `print_host':
nslookup.c:(.text.print_host+0x44): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
debianutils/lib.a(mktemp.o): In function `mktemp_main':
mktemp.c:(.text.mktemp_main+0x98): warning: the use of `mktemp' is dangerous, better use `mkstemp'
networking/lib.a(ipcalc.o): In function `ipcalc_main':
ipcalc.c:(.text.ipcalc_main+0x25c): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
libbb/lib.a(inet_common.o): In function `INET_resolve':
inet_common.c:(.text.INET_resolve+0x60): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(inetd.o): In function `reread_config_file':
inetd.c:(.text.reread_config_file+0x230): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(netstat.o): In function `ip_port_str':
netstat.c:(.text.ip_port_str+0x50): warning: Using 'getservbyport' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
coreutils/lib.a(sync.o): In function `sync_main':
sync.c:(.text.sync_main+0x7c): undefined reference to `syncfs'
collect2: ld returned 1 exit status
Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.
Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"
Makefile:717: recipe for target 'busybox_unstripped' failed
make: *** [busybox_unstripped] Error 1
解决办法:
make menuconfig
Coreutils--->sync选项去掉,重新make编译通过,生成了busybox可执行文件。