今天研究了一下busybox的编译。自己下了一个busybox-1.25.0的版本(直接从busybox官网上下载:https://busybox.net/downloads/),进行编译,遇到了一些问题,通过百度搜索和自己摸索,也成功解决了,详细记录如下:
首先交代一下系统版本和开发环境:
- 操作系统:ubuntu 12.04(64bit)
- 交叉编译工具链:arm-linux-gcc 4.4.3
- busybox源码包:busybox-1.25.0
一、修改Makefile配置
首先解压源码包:
tar -jxvf busybox-1.25.0.tar.bz2
- 1
进入busybox-1.25.0目录,修改Makefile文件如下:
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
- 1
- 2
二、修改配置文件
make menuconfig
- 1
选择Busybox Settings—>Build Options—>,选择[*] Build Busybox as a static binary(no shared libs);
三、编译源码,解决问题
make clean
make
- 1
- 2
出现如下错误:
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
解决办法:
MTD_FILE_MODE_RAW在/usr/include/mtd/mtd-abi.h中定义,于是将/usr/include/mtd/mtd-abi.h拷贝到busybox的include文件中,然后在nandwrite.c文件中包含该头文件:
gedit miscutils/nandwrite.c
- 1
修改
#include "libbb.h"
#include <mtd/mtd-user.h>
- 1
- 2
为
#include "libbb.h"
#include "mtd-abi.h"
#include <mtd/mtd-user.h>
- 1
- 2
- 3
此问题解决。继续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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
解决办法:
BLKSECDISCARD在/usr/include/linux/fs.h中定义,同上理,直接将/usr/include/linux/fs.h拷贝到busybox的include文件中,不过这次还要额外修改一下文件中的部分内容,否则会出现编译失败。
1、屏蔽以下4个头文件(否则编译时会提示找不到头文件):
//#include <linux/limits.h>
//#include <linux/ioctl.h>
//#include <linux/blk_types.h>
//#include <linux/types.h>
- 1
- 2
- 3
- 4
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
然后修改blkdiscard.c中对fs.h头文件的包含方式:
gedit util-linux/blkdiscard.c
- 1
修改
#include <linux/fs.h>
- 1
为
#include "fs.h"
- 1
继续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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
解决办法:
make menuconfig
- 1
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
解决办法:
make menuconfig
- 1
Coreutils—>sync选项去掉,重新make编译通过,终于顺利生成了busybox可执行文件。
OK,今天到此为止,明天再继续研究。
搞定,收工!