今天研究了一下busybox的编译。自己下了一个busybox-1.25.0的版本(直接从busybox官网上下载:https://busybox.net/downloads/),进行编译,遇到了一些问题,通过百度搜索和自己摸索,也成功解决了,详细记录如下:
首先交代一下系统版本和开发环境:
一、修改Makefile配置
首先解压源码包:
tar -jxvf busybox-1.25.0.tar.bz2
进入busybox-1.25.0目录,修改Makefile文件如下:
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
二、修改配置文件
make menuconfig
选择Busybox Settings—>Build Options—>,选择[*] Build Busybox as a static binary(no shared libs);
三、编译源码,解决问题
make clean
make
出现如下错误:
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中定义,于是将/usr/include/mtd/mtd-abi.h拷贝到busybox的include文件中,然后在nandwrite.c文件中包含该头文件:
gedit miscutils/nandwrite.c
修改
#include "libbb.h"
#include
为
#include "libbb.h"
#include "mtd-abi.h"
#include
此问题解决。继续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文件中,不过这次还要额外修改一下文件中的部分内容,否则会出现编译失败。
1、屏蔽以下4个头文件(否则编译时会提示找不到头文件):
//#include .h>
//#include
//#include _types.h>
//#include
2、屏蔽以下几个结构体(否则编译时会提示找不到类型定义):
#if 0
struct fstrim_range {
__u64 start;
__u64 len;
__u64 minlen;
};
/* And dynamically-tunable limits and defaults: */
struct files_stat_struct {
unsigned long nr_files; /* read only */
unsigned long nr_free_files; /* read only */
unsigned long max_files; /* tunable */
};
struct inodes_stat_t {
int nr_inodes;
int nr_unused;
int dummy[5]; /* padding for sysctl ABI compatibility */
};
#endif
然后修改blkdiscard.c中对fs.h头文件的包含方式:
gedit util-linux/blkdiscard.c
修改
#include
为
#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可执行文件。
OK,今天到此为止,明天再继续研究。
搞定,收工!
篇后语:
1、编译完后对上述几个问题进行反思,发现这几个问题其实来讲可以分为2类。一类是头文件包含的问题,一类是make menuconfig配置的问题。后者,可能跟每个人的配置需求有关。但是前者的问题,我认为busybox公司完全可以将那2个头文件直接包含在源码包中,将相关代码修改成OK的状态,让我们下载过来就可以直接编译通过,免去我们这番折腾。但是为何busybox公司没有这么做呢?其中应该是有原因的,暂时想不通,待日后再仔细琢磨吧。
2、本文撰写时,使用的busybox工具源码版本是busybox-1.25.0,使用上述方法验证通过。不过,紧接着,我又验证了最新的busybox-1.26.2版本,同样使用上述方法,顺利通过,编译成功。证明本文中的方法应该对于各个版本都是通用的。
3、本文只是简单的介绍了一下使用busybox过程中遇到几个编译出错的问题的解决办法,并未对整套编译流程进行详细介绍。关于如何利用busybox工具编译出linux根文件系统的完整流程,可参见我的另外一篇博客(如何使用busybox编译和生成最简linux根文件系统(rootfs))。